In any programming language, a variable is a memory location that holds a value. It could be a number, string, an object but at a time, it can contain only one value. What if you need 20 such values, or 50 or even 100?

One way is to declare and initialize those 100 variables. The alternative method is using a data structure known as an array to hold all those 100 values. An array can hold multiple values at a time in a key, value pair. To access an element in an array, you have to refer to the key of that element.

The arrays are useful to create a list of values of the same data type, which can be accessed using their index or key. An array is formed using an array() function in PHP. These arrays can store numbers, strings, and an object.

Array in PHP

No matter what kind of data type it is holding, if it is a numeric array, the index will always start from zero. Let’s say; an array has 100 integer numbers. The first number will be stored at zero index and the second element at first index and so on. The total number of items an array is also known as the size or length of an array.

Let’s understand arrays in PHP with a simple example:

PHP lets you create an array using array() function.

<?php

$arr = array("Hello", "world");
echo $arr[0]. " ". $arr[1];

?>

Output

Hello world

Types of Array

In PHP an array can store numbers or strings as indexes.

Numeric Arrays

In numeric arrays, an index is always a number. By default, the index starts at zero. You can create a numeric array using array() function or by storing individual values.

You can also create an array by storing values at indexes. For example

<?php

$arr[0] = 10;
$arr[1] = 20;
$arr[2] = 30;
$arr[3] = 40;
$arr[4] = 50;

?>

The above is a simple array with five values. The first value (10) resides at the zeroth index whereas the last value(50) is at index 4. The size or length of an array is 5. You can add as many elements you would like to have in an array.

To read a numeric array, use a For or a While loop.

<?php

 	for($i =0; $i<5; $i++)
     echo $arr[$i]."<br/> ";

?>

Output

The output of an Array

Associative arrays

The associate arrays work similar to the numeric arrays. The only difference is their indexes. An associative array has a string as their index which creates a semantic relationship between key and values.

Let’s say you have store the CGPA of students; you can store student IDs as keys of an array whereas the total CGPA earned by the student will be its respective values.

<?php

$arr["BSCS001"] = 3.27;
$arr["BSCS002"] = 2.80;
$arr["BSCS003"] = 3.40;
$arr["BSCS004"] = 4.00;
$arr["BSCS005"] = 2.74;

?>

In the above example, a student with ID BSCS001 has 3.27 CGPA, BSCS001 has 2.80 CGPA and so on.

Traversing of an array

To traverse an array, you can use any loop such as for, while, do while but in PHP the recommended loop for array traversal is foreach loop. The most significant advantage of the foreach loop is you don’t have to mention any indexes explicitly and still it will traverse the whole array.

<?php

$arr["BSCS001"] = 3.27;
$arr["BSCS002"] = 2.80;
$arr["BSCS003"] = 3.40;
$arr["BSCS004"] = 4.00;
$arr["BSCS005"] = 2.74;

foreach($arr as $key => $val){
    echo $key. " has earned ". $val. " CGPA";
    echo "<br/>";
}//foreach
?>

You can also store the values in the individual array elements.

 <?php
$arr = array("BSCS001" => 3.27,
             "BSCS002" => 2.80,
             "BSCS003" => 3.40,
             "BSCS004" => 4.00,
             "BSCS005" => 2.74);
     foreach($arr as $key => $val){
           echo $key. " has earned ". $val. " CGPA";
           echo "<br/>";
 }//foreach
 ?>

Output

The output of Student Array

You can also create an associate array of mixed type.

For example, the following code has used strings and numbers as indexes.

<?php 

$arr["str1"] = 1; 
$arr[0] = "abc"; 
$arr[1] = 2; 
$arr["str2"] = "def"; 


foreach ($arr as $key => $val){ 
echo $key." has ".$val."<br/>"; 
} 
?>

Array of mixed type

Recommended Reading

Last modified: March 19, 2019