Arrays in PHP

Arrays are used to store collection of items in a sequence. Consider the case where you have to store price of 100 items, in such cases it is not advised to create 100 variables, rather an Array of 100 items should be declared which will serve the purpose. Arrays are of three types:

  • Index Based Arrays
  • Associative Arrays
  • Multi-dimensional arrays

Index based arrays

Index based arrays are simplest of all the arrays. These arrays store items in terms of zero based index. The index of the first item is zero, while the index of last item is m-1 where m is the size of the array. Have a look at the following example to understand index based arrays.

<?php
    // First method of creating iindex arrays
    $colors = array("red", "green", "blue","yellow","white");
    foreach($colors as $color)
    {
        echo $color.'<br>';
    }

    //second method of creating index based arrays
    $colors[0] = "white";
    $colors[1] = "yellow";
    $colors[2] = "blue";
    $colors[3] = "green";
    $colors[4] = "red";

     foreach($colors as $color)
    {
        echo $color.'<br>';
    }
    
?>

Download the code Run the code

In the above code, two methods for defining index-based arrays have been explained. You can either use array class and add the items to it separated by comma or you can individually add the items by specifying index.

Associative arrays

In associative arrays, the index is a string. Associative arrays are more readable and are preferred over normal numeric index based arrays. Have a look at the following example to understand associative arrays.

<?php
    // First method of creating associative arrays
    $studentage = array("John"=>20, "Sara"=>25,
                        "Mike"=>21, "Shane"=>19, 
                        "Vicky"=>23 );

    echo 'Age of Sara is '.$studentage['Sara'].'<br>';
    echo 'Age of Mike is '.$studentage['Mike'].'<br>';

    //Second Method of creating associative array
    $studentage['Sara'] = 29;
    $studentage['Mike'] = 15;

    echo 'Age of Sara is '.$studentage['Sara'].'<br>';
    echo 'Age of Mike is '.$studentage['Mike'].'<br>';
?>

Download the code Run the code

In the above code, the name of the student has been passed as the index to the studentage array. Using this name as index, the age of Sara and Mike is being presented on the webpage.

Multi-Dimensional arrays

Multi-dimensional arrays are similar to simple arrays however in multi-dimensional arrays, each item of the array can be an array itself and the items of this child array can also be array. Multi-dimensional arrays can be created with any level of depth. In the following example, a 2 dimensional array $customers has been created. Each customer further contains an array which stores his age, city and bill. The process of creating such an array and accessing items within that array have been explained in the example.

<?php
    $customers = array(
					'Sally'=> array(
					'age'=>20,
					'city'=>"LA",
					'bill'=>200  
					),
			
					'Winsten'=> array(
					'age'=>27,
					'city'=>"Washington",
					'bill'=>500  
					)
                );
        
	echo $customers['Sally']['city'].'<br>';
	echo $customers['Winsten']['city'].'<br>';
?>

Download the code Run the code

<<< PHP Switch StatementLoops in PHP >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .