PHP Array Explanation

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 Explanation
PHP Arrays 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.



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.

  $fruits = array(“Apple”, “Dates”, “Orange”, “Banana”, “Mango”);
  for($i=0; $i<5;$i++){
    echo $fruits[$i].”,  ”;


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.

 $a = array(1=>"Apple",10=>"Banana", 20=>;"Mango" );
 echo "The value of 10th index is ".$a[10];



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.

$students=array(“Sayeed”=>910, “Faisal”=>915, “Shahin”=>920 );
foreach($students as $key => $value) {
  echo "Name: ".$key. ", Roll: ".$value;
  echo "<br>";


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.

                    “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>”;


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.

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, Tailwindcss, Bootstrap, JavaScript, jQuery, Vue.js, WordPress and Laravel framework. Follow Me: Facebook, Twitter and Linkedin.

Related Posts

Laravel First Time Installation on Windows 10/11

Laravel is one of the best full stack frameworks used to develop modern web application simply and securely. Day by day PHP developers are absorbing Laravel. So,…

PHP Delete Array Element with Built-in Function

Array is an important and mostly used data type in PHP programming. PHP programmer can manually add element to an array or delete element from an array…

PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a…

Function Call by Value and Call by Reference in PHP

More than thousands of built-in functions proves the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined…

PHP Variable Usage inside of Function

Besides more than thousands of built-in functions PHP provides facility to define custom/user defined function. How to define and call a user defined function was discussed in…

PHP Function Explanation

Real power of a programming language comes from its functions. More than thousands of built-in functions defined in PHP language proves the power of PHP language. Besides…

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.