PHP Arrays

An array is an special variable, which can hold more than one value at a time. In PHP, there are 3 different kinds of arrays and each array value is accessed using an ID which is called array index.

Types of Arrays:

  • 1D Array / Numeric array.
  • Associative arrays.
  • 2D Arrays.

PHP 1D Array / Numeric array

Single Dimensional array is a array with a numeric index. Values are stored and accessed in linear way.

Example 1: Single Dimensional Array.

<?php
    $place = array("Taj Mahal Agra", "Akshardham Delhi", "Hawa Mahal Jaipur", "Gateway of India Mumbai", "Charminar Hyderabad");
    echo "<h2> <u>Point of Interest: </u><br> <br> <li>$place[0]</li> <br> <li>$place[1]</li> <br> <li>$place[2]</li> <br> <li>$place[3]</li> <br> <li>$place[4]</li> </h2>";
?>

Example 2: Display all Array values using foreach loop.

<?php
    $numbers = array( 1, 2, 3, 4, 5);
    foreach( $numbers as $value )
    {
        echo "Value is $value <br />";
    }
?>

Example 3: Display all color values from array using foreach loop.

<?php
    $colors = array("red", "green", "blue", "yellow");
    foreach ($colors as $value)
    {
        echo "$value <br />";
    }
?> 
Count and sizeof function is use to calculate array length. Its Syntax is:
echo sizeof($web);
echo count($web); 

Associative array

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index number. Associative array will have their index as string so that you can establish a strong association between key and values.

Example 4: Associative array

<?php
    $salary = array("ram" => 20000, "shyam" => 10000, "mohan" => 5000);

    echo "Salary of Ram is ". $salary['ram'] . "<br />";
    echo "Salary of Shyam is ". $salary['shyam'] . "<br />";
    echo "Salary of Mohan is ". $salary['mohan'] . "<br />";

    $age = array("Ram"=>"35", "Shyam"=>"37", "Mohan"=>"43");

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

PHP 2D Array / Multidimensional Array

Multidimensional array containing one or more arrays and values are accessed using multiple indexes.

Example 5: Multidimensional array. Display 3 Student marks.

<?php
    $marks = array ( array("Ritu",80,90), array("Sania",55,95), array("Aarti",88,77) );
   
    echo $marks[0][0].": First Term: ".$marks[0][1].", Second Term: ".$marks[0][2].".<br>";
    echo $marks[1][0].": First Term: ".$marks[1][1].", Second Term: ".$marks[1][2].".<br>";
    echo $marks[2][0].": First Term: ".$marks[2][1].", Second Term: ".$marks[2][2].".<br>";
    echo $marks[3][0].": First Term: ".$marks[3][1].", Second Term: ".$marks[3][2].".<br>";
?>

PHP Arrays Sort Functions

Function Description
sort() Sort arrays in ascending order. Syntax: sort($array);
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
array_unique() Remove duplicate elements
array_search() returns the key for the first match in an array

Exercise:

Exercise 1: Demo Download