Array is one of the most important and basic terms in PHP Language. Almost all the PHP program uses array data structure. So, those who want to be expert in PHP programming should have good understanding on PHP array.
In my last articles, I explained some PHP basic fundamentals like PHP variables, PHP conditional statements, PHP looping statements and PHP function. In this article, I am going to explain PHP array data structure elaborately with example.

PHP Array
PHP array is a special variable that can hold one or more value at a time in a single variable. Multiple values from an array are retrieved with array indexes. For example, the following PHP array contains five numeric values in a single variable.
$numbers = array(10, 20, 30,40,50);
These array values are retrieved using array indexes. Array index is by default start with 0 and increase by one if we do not define array index explicitly.
How to Declare PHP Array
PHP has a built-in function named array() which is used to create array data. The basic structure of declaring a PHP array is:
$a = array (index1=>value1, index2=>value);
Here, $a is an array variable that holds all the elements created by array() function. Values in an array are items those will be kept in that array. In PHP, values can be any data value such as numeric value, string etc.
Index is used to retrieve array elements/items from array data. The (=>) operator refers to assign value on the indexed location of an array.
We can also declare PHP array like the following format.
$a[index1]=value1;
$a[index2]=value2;
Both array declaring methods will do the same thing but with the array() function, we can create multiple array elements at a time.
Types of PHP Array
PHP Array is classified based on array indexes. Based on PHP array index data, array is classified either numeric array or associative array. On the other hand, based on array deep level array can also be classified either one-dimensional array or multidimensional array.
PHP Numeric Array
If array index is defined with integer value, the array is called Numeric Array. Numeric array is the simplest form of PHP array. If we do not declare array index explicitly, PHP considered it is a numeric array. Array indexes of a numeric array are start with 0 and then increased by one by default. It is also possible to assign numeric array indexes arbitrary.
Example of PHP Numeric Array
The following example is a simple PHP array declaration where five string values is kept in a numeric array and then retrieving the numeric array values with PHP for loop.
<?php
$fruits = array(“Apple”, “Dates”, “Orange”, “Banana”, “Mango”);
for($i=0; $i<5;$i++){
echo $fruits[$i].”, ”;
}
?>
Output
Apple, Dates, Orange, Banana, Mango
Again, if we wish, we can assign numeric array index explicitly like the following program and then retrieve any specific value with index value. Look carefully, here we have assigned index value arbitrary.
<?php
$a = array(1=>"Apple",10=>"Banana", 20=>;"Mango" );
echo "The value of 10th index is ".$a[10];
?>
Output
Banana
PHP Associative Array
If array index is represented with string value, the array is called associative array. Associative array is one of the frequently used array types in PHP programming.
Example of PHP Associative Array
The following program is an example of PHP associative array where students’ rolls are kept within an associative array and array indexes are expressed with students’ name. The easiest way of retrieving associative array values is using foreach loop. So, foreach loop is being used to retrieve the following associative array elements.
<?php
$students=array(“Sayeed”=>910, “Faisal”=>915, “Shahin”=>920 );
foreach($students as $key => $value) {
echo "Name: ".$key. ", Roll: ".$value;
echo "<br>";
}
?>
Output
Name: Sayeed, Roll: 910
Name: Faisal, Roll: 915
Name: Shahin, Roll: 920
PHP One-Dimensional Array/Linear Array
If any element of an array can be retrieved using only one index, the array is called one-dimensional array. It is also known as linear array. One dimensional array is a simple array declaration of PHP. The above two programs those we have leaned are example of one dimensional array. So, I am not giving more examples here. In the following section we will learn about PHP multidimensional array.
PHP Multidimensional Array
If any array element is retrieved using more than one index, the array is called multidimensional array. On the other hand, we can say, if any array contains another array, the array will be multidimensional array. PHP supports multidimensional arrays that can be two, three, four, five, or more level deep. However, arrays which are more than three level deep are hard to manage for the most people.
Example of multidimensional Array
The following program is an example of multidimensional array where students’ information is organized based on Class Roll with two dimensional array fashions.
<?php
$students=array(
“910” => array(“Name”=>“Sayeed”, “Age”=>22),
“915” => array(“Name”=>“Kabir”, “Age”=>23),
);
foreach($students as $roll => $student){
echo “Roll: ”.$roll.”<br>”
foreach ($student as $key => $value){
echo $key.”: ”.$value.” , ”;
}
echo “<br>”;
}
Output
Roll: 910
Name: Sayeed, Age: 22
Roll: 915
Name: Kabir, Age: 23
To retrieve multidimensional array elements, we have to use multiple loop statements according to array deep level. For example, if we have two-dimensional array, we need to use two loop statements. Again, if we have three-dimensional array, we need to use three loop statements.
PHP Array with its type has been explained in this article. I hope, you have now better understanding on PHP array. However, if you face any confusion to understand PHP array, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you.