PHP Tutorial: PHP Arrays

PHP Arrays

An array stores multiple values in one single variable:
 

Advantage of PHP Array:-

  • Less Code: We don't need to define multiple variables.
  • Easy to traverse: By the help of single loop, we can traverse all the elements of an array.
  • Sorting: We can sort the elements of array.

PHP Array Types
There are 3 types of array in PHP.

  • Indexed Array
  • Associative Array
  • Multidimensional Array

PHP Indexed Array
PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0.
PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.


Definition
There are two ways to define indexed array:

1st:

$size=array("Big","Medium","Short");  
2nd way:
$size[0]="Big";  
$size[1]="Medium";  
$size[2]="Short";  
 

Example

<?php  
$size=array("Big","Medium","Short");  
echo "Size: $size[0], $size[1] and $size[2]";  
?>

Output:
Size: Big, Medium and Short

<?php  
$size[0]="Big";  
$size[1]="Medium";  
$size[2]="Short";  
echo "Size: $size[0], $size[1] and $size[2]";  
?>  

Output:
Size: Big, Medium and Short

Traversing PHP Indexed Array

We can easily traverse array in PHP using foreach loop. Let's see a simple example to traverse all the elements of PHP array.

<?php  
$size=array("Big","Medium","Short");  
foreach( $size as $s )  
{  
  echo "Size is: $s<br />";  
}  
?>  

Output:
Size is: Big
Size is: Medium
Size is: Short

Count Length of PHP Indexed Array
PHP provides count() function which returns length of an array.

<?php  
$size=array("Big","Medium","Short");  
echo count($size);  
?>  

Output:
3

associative arrays

In an associative array a key is associated with a value. If you wanted to store the salaries of your employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.
Definition
There are two ways to define associative array:

1st:

$salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");  

2nd:
$salary["Sonoo"]="550000";  
$salary["Vimal"]="250000";  
$salary["Ratan"]="200000";  
Example

<?php    
$salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");  
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";  
echo "Vimal salary: ".$salary["Vimal"]."<br/>";  
echo "Ratan salary: ".$salary["Ratan"]."<br/>";  
?>    

Output:
Sonoo salary: 550000
Vimal salary: 250000
Ratan salary: 200000

<?php    
$salary["Sonoo"]="550000";  
$salary["Vimal"]="250000";  
$salary["Ratan"]="200000";   
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";  
echo "Vimal salary: ".$salary["Vimal"]."<br/>";  
echo "Ratan salary: ".$salary["Ratan"]."<br/>";  
?>    

Output:
Sonoo salary: 550000
Vimal salary: 250000
Ratan salary: 200000

Traversing PHP Associative Array

By the help of PHP for each loop, we can easily traverse the elements of PHP associative array.

<?php    
$salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");  
foreach($salary as $k => $v) {  
echo "Key: ".$k." Value: ".$v."<br/>";  
}  
?>    

Output:
Key: Sonoo Value: 550000
Key: Vimal Value: 250000
Key: Ratan Value: 200000

PHP Multidimensional Array
PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.
 

Definition
$emp = array  
  (  
  array(1,"sonoo",400000),  
  array(2,"john",500000),  
  array(3,"rahul",300000)  
  );  
Example
Let's see a simple example of PHP multidimensional array to display following tabular data. In this example, we are displaying 3 rows and 3 columns.

Id Name Salary
1 Sonu 40000
2 hulk 50000
3 Nita 35000
<?php    
$emp = array  
  (  
  array(1,"sonoo",400000),  
  array(2,"john",500000),  
  array(3,"rahul",300000)  
  );  
  
for ($row = 0; $row < 3; $row++) {  
  for ($col = 0; $col < 3; $col++) {  
    echo $emp[$row][$col]."  ";  
  }  
  echo "<br/>";  
}  
?>    

Output:
1 sonu 400000 
2 hulk 500000 
3 Nita 300000 

PHP 5 Sorting Arrays

The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.
PHP - Sort Functions For Arrays:-

  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key

Sort Array in Ascending Order - sort()

The following example sorts the elements of the $cars array in ascending alphabetical order:
Example

<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);

$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
    echo $cars[$x];
    echo "<br>";
}
?>

Output:
BMW
Toyota
Volvo

The following example sorts the elements of the $numbers array in ascending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);

$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
    echo $numbers[$x];
    echo "<br>";
}
?>

Output:
2
4
6
11
22

Sort Array in Descending Order - rsort()
The following example sorts the elements of the $cars array in descending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);

$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
    echo $cars[$x];
    echo "<br>";
}
?>

Output: 
Volvo
Toyota
BMW

The following example sorts the elements of the $numbers array in descending numerical order:
 

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);

$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
    echo $numbers[$x];
    echo "<br>";
}
?>

Output:
22
11
6
4
2

Sort Array (Ascending Order), According to Value - asort()
The following example sorts an associative array in ascending order, according to the value:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);

foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

Output: 
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

Sort Array (Ascending Order), According to Key - ksort()
The following example sorts an associative array in ascending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);

foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

Output:
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35

Sort Array (Descending Order), According to Value - arsort()
The following example sorts an associative array in descending order, according to the value:

<?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>";
}
?>

Output:
Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35

Sort Array (Descending Order), According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);

foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

Output:
Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37

Next Topic