PHP Arrays | Indexed, Associative and Multidimensional PHP Arrays

PHP Arrays

Arrays are one of the most important elements of a programming language. They are used to solve various problems where you want to store multiple values in a single variable. No programming language can survive without arrays.

What is an Array?

Arrays are variables that can hold multiple values at a time. For example if you have a list of mobile phones without using arrays you would store the values like this:

$mp1 = “iPhone”;
$mp2 = “HTC”;
$mp3 = “Huawei”;

It looks quite easy, but think if you have 200 hundred various type of mobiles and you want to find one mobile phone from this list. It would be harder and harder if the list will increase, so how to solve this problem. This problem is solved by PHP arrays.

Arrays have indexes to access their values with a one single array name.

How to Create PHP arrays?

In PHP arrays are create with two methods

  • The use of array() to create an array
  • The of [] (in PHP 5.4.0 or above)

$mobiles_array = array();

Or

$mobiles_array = []; //(in PHP 5.4.0 or above)

Type of PHP arrays

There are 3 different types of arrays in PHP:

  • Indexed arrays (They have numeric indexes)
  • Associative arrays (They have named keys)
  • Multidimensional arrays (They have arrays as indexes or they contain more sub arrays)

PHP Indexed Arrays

There are three ways to create indexed arrays in PHP:

Note: The PHP arrays are 0 base indexed, mean the index start from the 0 not 1.

Here is a mobiles PHP array example:

$mobiles = array(“Nokia”, “HTC”, “iPhone”, “SamSung”);

Here the first element is $mobiles[0] and the last one will be $mobiles[3].

The second method can be used in PHP 5.4.0 or above:

$mobiles = ["Nokia", "HTC", "iPhone", "SamSung"];

Same rule applies here the first element is $mobiles[0] and the last one will be $mobiles[3].

The third method is by giving the values to each array element manually.

$mobiles[0] = "Nokia";
$mobiles[1] = "HTC";
$mobiles[2] = "iPhone";
$mobiles[3] = "SamSung";

$mobiles = array("Nokia", "HTC", "iPhone", "SamSung");
echo "The best mobile brands " . $mobiles[0] . ", " . $mobiles[1] . ", " . $mobiles[2] . " and " . $mobiles[3] . ".";

The Array count() function

The count() function is one of the most used PHP array function. It is used to get the length of an array or to find how many elements are there in an array.

Array count() function example:

<?PHP
$mobiles = array("Nokia", "HTC", "iPhone", "SamSung");
echo count($mobiles);// result will be 4

Looping an Indexed PHP Array

There are two ways to loop through a PHP Indexed array

  • Loop through indexed array using “For” loop
  • Loop through indexed array using “Foreach” loop

 

<?php
$mobiles = array("Nokia", "HTC", "iPhone", "SamSung");
$total_mobiles = count($mobiles);

for($i = 0; $i < $total_mobiles; $i++) {
    echo $mobiles[$i]."<br>";
}
?>

In the above example we have used a count function and a for loop to display each array element.

 

<?php
$mobiles = array("Nokia", "HTC", "iPhone", "SamSung");

foreach($mobiles as $mobile){
	echo $mobile."<br>";
}
?>

In this example we have used a foreach loop to print out all the array elements.

PHP Associative Arrays

The associative arrays have key and value structure. In the associative arrays you can assign your own keys and values to array elements.

There are three ways to create an associative array:

 $sites_rank = array("Facebook" => "3", "Yotube" => "2", "Google" => "1");

Or:

$sites_rank = ["Facebook" => "3", "Yotube" => "2", "Google" => "1"];// (In PHP 5.4.0

or above);

$sites_rank["Google"] = "1";

$sites_rank["Yotube"] = "2";

$sites_rank["Facebook"] = "3";

The order doesn’t matter in this array.

Loop Through an Associative Array

To loop through an associative array you can use a foreach loop.

<?php
$sites_rank = array("Facebook" => "3", "Yotube" => "2", "Google" => "1");

foreach($sites_rank as $key => $value) {
    echo "Key=" . $key . ", Value=" . $value;
    echo "<br>";
}
?>

PHP Multidimensional Arrays

Multidimensional arrays are those arrays that can hold more than one array. There can be many levels of multidimensional arrays like two dimensional arrays mean there are two arrays in an array, three dimensional arrays and so on.

The more dimensions an array has the more indices you have to use to access the array elements.

  • For a 2-dimensional array two indices will be used to select an element
  • For a 3-dimensional array three indices will be used to select an element
<?PHP $movies = array( array( "title" => "Full Metal Jacket",
    "director" => "Stanley Kubrick",
    "year" => 1987
  ),
  array(
    "title" => "Rear Window",
    "director" => "Alfred Hitchcock",
    "year" => 1954
  ),
  array(
    "title" => "Mean Streets",
    "director" => "Martin Scorsese",
    "year" => 1973
  )
);

To access the movies titles in the above array:
echo $movies[0]["title"]."
";
echo $movies[1]["title"]."
";
echo $movies[2]["title"]."
";
?>

Comments