Array
An array stores multiple values in one single variable.A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.An array is a special variable, which can store multiple values in one single variable.An array can hold all your variable values under a single name. And you can access the values by referring to the array name.Each element in the array has its own index so that it can be easily accessed.
Create an Array in PHP
In PHP, the array() function is used to create an array.
Types of Array in PHP
There are three types of array in php, which are given below.
- Indexed/Numeric arrays – Arrays with a numeric index
- Associative arrays – Arrays with named keys
- Multidimensional arrays – Arrays containing one or more arrays
i. Numeric Arrays
A numeric array stores each array element with a numeric index. There are two methods to create a numeric array.
- In the following example the index are automatically assigned (the index starts at 0):
1
$cars=array("Saab","Volvo","BMW","Toyota");
2. In the following example we assign the index manually:
123
$cars[0] = "Volvo";$cars[1] = "BMW";$cars[2] = "Toyota";
Example
1234
<?php$student = array("Harry", "Varsha", "Gaurav");echo "Class 10th Students " . $student[0] . ", " . $student[1] . " and " . $student[2] . ".";?>
output
Class 10th Students Harry, Varsha and Gaurav
ii.Associative Arrays
An associative array, each ID key is associated with a value.When storing data about specific named values, a numerical array is not always the best way to do it.With associative arrays we can use the values as keys and assign values to them.
syntax :-
$age = array(“Harry”=>”10”, “Varsha”=>”20”, “Gaurav”=>”30”);
or
$age[‘Harry’] = “10”;
$age[‘Varsha’] = “20”;
$age[‘Gaurav’] = “30”;
iii. multidimensional array
A multidimensional array is an array containing one or more arrays. For a two-dimensional array you need two indices to select an element
Array Example
1234567891011121314
<?php$student = array(array("Harry",300,11),array("Varsha",400,10),array("Gaurav",200,8),array("Hitesh",220,8));$student = array("Harry", "Varsha", "Gaurav");echo $student[0][0].": Marks: ".$student[0][1].", Class: ".$student[0][2].".<br>";echo $student[1][0].": Marks: ".$student[1][1].", Class: ".$student[1][2].".<br>";echo $student[2][0].": Marks: ".$student[2][1].", Class: ".$student[2][2].".<br>";echo $student[3][0].": Marks: ".$student[3][1].", Class: ".$student[3][2].".<br>";?>
output
Harry: Marks: 300 Class: 11
Varsha: Marks: 400 Class: 10
Gaurav: Marks: 200 Class: 8
Hitesh: Marks: 220 Class: 8
Sorting an Array
Ordering data in an increasing or decreasing fashion.
i.Sort: Sorting an index array in ascending order.
12345678910
<?php$num=array(2,5,3,4,1);sort($num);$length=count($num);for($i=0;$i<$length;$i++){ echo $num[$i]; echo "<br>";} ?>
ii.rsort: Sorting an index array in descending order.
12345678910
<?php$num=array(2,5,3,4,1);rsort($num);$length=count($num);for($i=0;$i<$length;$i++){ echo $num[$i]; echo "<br>"; }?>
iii.asort: Sorting an associative array in ascending order according to value.
1234567891011
<?php$age=array("Ram"=>29,"Hari"=>39,"Gopal"=>19);asort($age);$length=count($age);foreach($age as $key=>$value){ echo "Key : ".$key." & Value : ".$value; echo"<br>"; }?>
iv.ksort: Sorting an associative array in ascending order according to key.
123456789
<?php$age=array("Ram"=>29,"Hari"=>39,"Gopal"=>19);$length=count($age);ksort($age);foreach($age as $key=>$value){ echo "Key : ".$key." & Value : ".$value; echo"<br>"; }?>
v. arsort: Sorting an associative array in descending order according their value.
12345678910
<?php $employee=array("Ram"=>1,"Hari"=>2,"Gopal"=>5,"Ramila"=>3, arsort($employee);"kundhan"=>4,); $length=count($employee);foreach($employee as $key=>$value){ echo "Key : ".$key." & Value : ".$value; echo"<br>"; }?>
vi. krsort: Sorting an associative array in descending order according their key.
12345678
<?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); arsort($age);foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>";}?>