Array variables are used in PHP to store multiple values in a variable, and the values can be accessed using indexes or keys. The index of the array can be numeric or associative. Two types of array can be declared in PHP. One is a one-dimensional array, and another is a multi-dimensional array. When the array contains more than one dimension, then it is called a multi-dimensional array. A two-dimensional array is one type of multi-dimensional array that has two dimensions. Tabular data are stored in a two-dimensional array that contains a fixed number of rows and columns. How a two-dimensional array can be declared and used is shown in this tutorial.

Example 1: Two-dimensional numeric array

First, create a PHP file with the following script to know the way of defining a two-dimensional numeric array where the row values are numeric. An array named $books is declared in the script, and it contains 5 rows and 3 columns. The indexes of the row and column of the array are numeric here. Next, two ‘for‘ loops are used to iterate the rows and columns of the array and print the content of the output in tabular form.

<?php

/* Define a two-dimensional numeric array of 5 rows and 3 columns */

$books = array (array(’01’ ,‘PHP MYSQL In 8 Hours, For Beginners, Learn Coding Fast!’ , ‘Ray Yao’),


                array(’02’ ,‘Learn PHP and MySQL with AJAX in a weekend’ , ‘Blerton Abazi’),


                array(’03’ ,‘Domain-Driven Design in PHP’ , ‘Carlos Buenosvinos, Christian Soronellas, Keyvan Akbary’),


                array(’04’ ,‘PHP 7 Quick Scripting Reference’ , ‘Mikael Olsson’),


                array(’05’ ,‘Jump Start PHP Environment’ , ‘Bruno Skvorc’));

/* Set the title of the table */

echo

PHP Book List

;

/* Set the heading of the table */

echo

;

/* Use the loop to iterate the five rows of the array */

for ($row = 0; $row < 5; $row ) {


    echo

;

    /* Use the loop to iterate the three columns of the array */


    for ($col = 0; $col < 3; $col ) {

        /* Read the value of the array based on row and column values */  


        echo

;


    }


    echo

;


 

}

echo

ID Book Name Author Name
.$books[$row][$col].

;

?>

Output:


The following output will appear after running the script from the webserver.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/use-of-two-dimensional-array-in-php_1.jpg" data-lazy- height="452" src="data:image/svg xml,” width=”1278″>

Example 2: Two-dimensional associative array where the index of the row is the string

Create a PHP file with the following script to know the way of defining a two-dimensional associative array where the index of the row is a string. The array defined in the script contains 5 rows and 4 columns. The string key is used for the array that contains the row values, and the numeric key is used for the array that contains the column values. ‘foreach‘ loop is used to iterate the rows, and the ‘for‘ loop is used to iterate the columns of the array. The content of the two-dimensional array will be printed in the tabular form like the previous example.

<?php

/* Define a two-dimensional associative array of 5 rows and 3 columns */

$softwares = array(

‘OS’=>array (“Ubuntu”, “Windows 8”, “Windows 10”, “Frdora”),

‘Antivirus’=>array (“Norton”, “Avast”, “Avira”, “Panda”),

‘Video Player’=>array (“VLC”,“KMPlayer”, “5kplayer”,“Miro”),

‘Browser’=>array (“Firefox”,“Chrome”, “Opera”,“Edge”),

‘Word Processor’=>array (“LibreOffice Writer”,“Microsoft Word”, “WPS Office Free Writer”,“FocusWriter”)

);

/* Print the particular value of two-dimensional associative array */

echo

.$softwares[‘Antivirus’][1].” is a an antivirus software.

;

/* Print all column values of the particular row */

echo

The list of video players are:

;

/* Use the loop to iterate the columns of the array based on the particular row */

for ($j = 0; $j < 4; $j ) {


   echo $softwares[‘Video Player’][$j].
;

}

/* Set the title of the table */

echo

Software List

;

/* Set the heading of the table */

echo

;

/* Use the loop to iterate the rows of the array */

foreach($softwares as $key => $values)

{


    echo

;

    /* Use the loop to iterate the columns of the array */


    for ($j = 0; $j < 4; $j ) {

        /* Read the value of the array based on row and column values */  


        echo

;


    }


    echo

;


 

}

echo

Software Type 1 2 3 4
.$key. .$softwares[$key][$j].

;

?>

Output:


The following output will appear after running the script from the webserver.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/use-of-two-dimensional-array-in-php_2.jpg" data-lazy- height="672" src="data:image/svg xml,” width=”1275″>

Example 3: Two-dimensional associative array where the indexes of row and column are string

Create a PHP file with the following script to know the way of defining the two-dimensional array where the index of both row and column is a string. Two ‘foreach‘ loops are used in the script to read the key values of the rows and the columns. The content of the array will be printed in the tabular form like the previous example.

<?php

/* Define a two-dimensional associative array where each row is another associative array */

$marks = array(

‘0117856’=>array (“CSE-101”=>78, “CSE-206”=>90, “CSE-208”=>80, “CSE-303”=>76),

‘0117858’=>array (“CSE-101”=>87, “CSE-206”=>79, “CSE-208”=>83, “CSE-303”=>66),

‘0117862’=>array (“CSE-101”=>71, “CSE-206”=>66, “CSE-208”=>75, “CSE-303”=>56),

‘0117865’=>array (“CSE-101”=>69, “CSE-206”=>70, “CSE-208”=>64, “CSE-303”=>59));

/* Set the title of the table */

echo

Result

;

/* Set the heading of the table */

echo

;

foreach($marks as $ids)

{


    foreach($ids as $key => $val)


        echo

;


    break;

}

echo

;

/* Use the loop to iterate the rows of the array */

foreach($marks as $key => $values)

{


    echo

;

    /* Use the loop to iterate the columns of the array */


    foreach($values as $v) {

        /* Read the values of the inner arrays */  


        echo

;


    }


    echo

;


 

}

echo

ID .$key.
.$key. .$v.

;

?>

Output:


The following output will appear after running the script from the webserver.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/use-of-two-dimensional-array-in-php_3.jpg" data-lazy- height="390" src="data:image/svg xml,” width=”1280″>

Conclusion

Different ways of declaring and accessing two-dimensional arrays are shown in this tutorial using multiple examples. The uses of both numeric and associative two-dimensional arrays are explained here to help the readers understand the concept of the two-dimensional array and apply it in PHP script for various purposes.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/channel-logo-150×150.jpg5fde13e462035.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.