PHP Article

PHP Arrays

This article will explain the different types of Arrays;
Indexed, Associative, and Multidimensional arrays and how to sort them.

This article assumes you have basic knowledge of HTML and PHP

Just what is an Array you ask?

An array is a variable which can hold more than one value at a time.

If we have a number of variables we would normally write it like this

$color1 = "Red";
$color2 = "White";
$color3 = "Blue";
                    

When we have more than one variable of the same type as in the example above we can use an array like this:

                    
<?php
$colors = array("Red", "White", "Blue");
echo "My favorite colors are " . $colors[0] . ", " . $colors[1] . ", and " . $colors[2] . ".";
?>

OUTPUT:
My favorite colors are Red, White, and Blue.                    
                

Indexed Arrays

There are two ways to create indexed arrays:
The index can be assigned automatically as in the example above, like this:

$colors = array("Red","White","Blue");
or the index can be assigned manually like this:

$colors[0] = "Red";
$colors[1] = "White";
$colors[2] = "Blue";
                    

NOTE: index always starts at 0

Loop Through an Indexed Array

To loop through all the values of an indexed array, you can use the count() function to get the length of the array then use a for loop, like this:

                    
<?php
$colors = array("Red", "White", "Blue");
$arrlength = count($colors);

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

OUTPUT:
Red
White
Blue                    
                

Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.

There are two ways to create an associative array:

$colors = array("First"=>"Red", "Second"=>"White", "Third"=>"Blue");
and

$colors['First'] = "Red";
$colors['Second'] = "White";
$colors['Third'] = "Blue";
                   

The named keys can be accessed like this:

                    
<?php
$colors = array("First"=>"Red", "Second"=>"White", "Third"=>"Blue");
echo "My favorite color is " . $colors['Third'] . "!";
?>

OUTPUT:
My favorite color is Blue!                    
                

Loop Through an Associative Array

To loop through all the values of an associative array, you can use the foreach loop, like this:

                    
<?php
$colors = array("First"=>"Red", "Second"=>"White", "Third"=>"Blue");

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

OUTPUT:
Key=First, Value=Red
Key=Second, Value=White
Key=Third, Value=Blue                    
                

Multidimensional Arrays

A multidimensional array is an array of arrays.
The number of arrays can be any level deep although more than three levels deep can be hard to manage!

NOTE: The dimension of an array indicates the number of indices you need to select an element.

  • For a two-dimensional array you need two indices to select an element
  • For a three-dimensional array you need three indices to select an element
  • and so on...
Look at the following table.
Color Likes Dislikes
Red 43 26
White 78 37
Blue 47 56

We can store the data from the table above in a two-dimensional array, like this:

                    
$colors = array (
  array("Red",43,26),
  array("White",78,37),
  array("Blue",47,56)
);                    
                

The two-dimensional $colors array contains three arrays, and it has two indices: row and column.

To get access to the elements of the $colors array we must point to the two indices (row and column):

                    
<?php
echo $colors[0][0].": Likes: ".$colors[0][1].", Dislikes: ".$colors[0][2].".<br>";
echo $colors[1][0].": Likes: ".$colors[1][1].", Dislikes: ".$colors[1][2].".<br>";
echo $colors[2][0].": Likes: ".$colors[2][1].", Dislikes: ".$colors[2][2].".<br>";
?>

OUTPUT:
Red: Likes: 43, Dislikes: 26.
White: Likes: 78, Dislikes: 37.
Blue: Likes: 47, Dislikes: 56.                    
                

We can also use two for loops, one inside another, to get the elements of the $colors array:

                    
<?php
for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";

  for ($col = 0; $col < 3; $col++) {
    echo $colors[$row][$col]."<br>";
  }
}
?>

OUTPUT:
Row number 0

Red
43
26

Row number 1

White
78
37

Row number 2

Blue
47
56                    
                

Sort Functions For Arrays

There are six sort functions:

  • 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()

This example sorts the $colors array in ascending alphabetical order:

                    
<?php
$colors = array("Red", "White", "Blue");
sort($colors);
?>

OUTPUT:
Blue
Red
White                    
                

Sort Array in descending Order - rsort()

This example sorts the $colors array in descending alphabetical order:

                    
<?php
$colors = array("Red", "White", "Blue");
rsort($colors);
?>

OUTPUT:
White
Red
Blue                    
                

Sort numeric Array in Ascending Order - sort()

This example sorts the $numbers array in ascending numerical order:

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

OUTPUT:
2
4
6
11
22                    
                

Sort numeric Array in descending Order - rsort()

This example sorts the $numbers array in descending numerical order:

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

OUTPUT:
22
11
6
4
2                    
                

Sort alphanumeric Array in Ascending Order - sort()

This example sorts the $alphanumeric array in ascending alphanumerical order:
It will sort by number first.

                    
<?php
$alphanumeric = array("A1", "2B", "D3D","4F0");
sort($alphanumeric);
?>

OUTPUT:
2B
4F0
A1
D3D                    
                

Sort alphanumeric Array in Descending Order - rsort()

This example sorts the $alphanumeric array in descending alphanumerical order:

                    
<?php
$alphanumeric = array("A1", "2B", "D3D","4F0");
rsort($alphanumeric);
?>

OUTPUT:
D3D
A1
4F0
2B                    
                

Sort Array in ascending order according to Value - asort()

This example sorts the array in ascending order according to Value - asort()

                    
<?php
$colors = array("First"=>"Red", "Second"=>"White", "Third"=>"Blue");
asort($colors);

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

OUTPUT:
Key=Third, Value=Blue
Key=First, Value=Red
Key=Second, Value=White                    
                

Sort Array in ascending order according to Key - ksort()

This example sorts the array in ascending order according to Key - ksort()

                    
<?php
$colors = array("First"=>"Red", "Second"=>"White", "Third"=>"Blue");
ksort($colors);

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

OUTPUT:
Key=First, Value=Red
Key=Second, Value=White
Key=Third, Value=Blue                    
                

Sort Array in descending order according to Value - arsort()

This example sorts the array in descending order according to Value - arsort()

                    
<?php
$colors = array("First"=>"Red", "Second"=>"White", "Third"=>"Blue");
arsort($colors);

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

OUTPUT:
Key=Second, Value=White
Key=First, Value=Red
Key=Third, Value=Blue                    
                

Sort Array in descending order according to Key - krsort()

This example sorts the array in descending order according to Key - krsort()

                    
<?php
$colors = array("First"=>"Red", "Second"=>"White", "Third"=>"Blue");
krsort($colors);

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

OUTPUT:
Key=Third, Value=Blue
Key=Second, Value=White
Key=First, Value=Red                    
                

I hope I have explained it well. Happy Coding!