Arrays in PHP

An array is a data structure that stores similar type of values in a common variable. Arrays are also called homogeneous data type.

For example if you want to store 50 numbers then instead of defining variables it is easy to define an array with size of 50.

In PHP, the array() function is used to create an array

Arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

$x=array(“laxmi”,”hirdesh”,”rahim”,”ashu”,”amit”);

// To count number of elements stored in array x

echo count($x);

// To populate value from any specific index or position

//echo $x[1];

// output – Hirdesh

// To print all elements in array x – using for-each loop

echo ” <br>Names are :”;

foreach ($x as $val)

{

echo  $val . ” “;

}

echo “<br> using for loop <br>”;

50

for($i=0; $i<count($x); $i++)

{

echo $x[$i] . “<br>”;

}

Sorting Arrays: PHP provides many easy ways to sort array values in predefined orders-

arsort: Sorts the array in descending value order and maintains the key/value relationship

asort: Sorts the array in ascending value order and maintains the key/value relationship

rsort: Sorts the array in descending value order

sort: Sorts the array in ascending value order

// Examples – Sorting of arrays

$x=array(“laxmi”,”hirdesh”,”rahim”,”ashu”,”amit”, “mohit”);

// rsort for DESC and simply sort for ASC

sort($x);

foreach ($x as $val)

{

echo  $val . ” “;

}

php book webs jyoti
php book webs jyoti

Leave a Reply

Your email address will not be published. Required fields are marked *

About Us

Luckily friends do ashamed to do suppose. Tried meant mr smile so. Exquisite behaviour as to middleton perfectly. Chicken no wishing waiting am. Say concerns dwelling graceful.

Services

Most Recent Posts

  • All Post
  • advanced-excel-training
  • Branding
  • deepti saini model
  • Development
  • Digital Marketing
  • digital marketing course gurgaon
  • Digital Marketing Internship Gurugram
  • digital marketing jobs
  • digital-marketing-institutes-sector-14
  • Leadership
  • Management
  • Notice
  • php-development-courses
  • php-full-stack-development-courses
  • php-job-opportunity
  • php-training
  • php-training-in-baksar
  • php-training-in-balia
  • php-training-in-bangalore
  • php-training-in-delhi
  • php-training-in-faridabad
  • php-training-in-gaziabad
  • php-training-in-gorakhpur
  • php-training-in-gurgaon
  • php-training-in-kolkata
  • php-training-in-muzaffarpur
  • php-training-in-noida
  • php-training-in-patna
  • placements
  • web development training gurgaon

Company Info

She wholly fat who window extent either formal. Removing welcomed.

Limited Seats Available – Register Now

We keep batch sizes limited so every learner gets personal attention and proper doubt solving. Reserve your seat now and start your training in the next batch without delay. For details call us +91- 8802000175

Help

Privacy Policy

Terms

Conditions

Subscribe Now

For latest news and updates, fill your details below-

You have been successfully Subscribed! Ops! Something went wrong, please try again.
© 2023 Created with Royal Elementor Addons