23 C
Jaipur
Friday, March 5, 2021

Use of two-dimensional array in PHP – Linux Hint

Must read

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 “<h2><p><b>PHP Book List</b></p></h2>”;

/* Set the heading of the table */
echo “<table border=1><tr><th>ID</th><th>Book Name</th><th>Author Name</th></tr>”;

/* Use the loop to iterate the five rows of the array */
for ($row = 0; $row < 5; $row++) {
    echo “<tr>”;

    /* 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 “<td>”.$books[$row][$col].“</td>”;
    }
    echo “</tr>”;
 
}
echo “</table>”;
?>

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

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 “<h3>”.$softwares[‘Antivirus’][1].” is a an antivirus software.</h3>”;

/* Print all column values of the particular row */
echo “<p>The list of video players are:</p>”;
/* 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].“<br />”;
}

/* Set the title of the table */
echo “<h2>Software List</h2>”;

/* Set the heading of the table */
echo “<table border=1><tr><th>Software Type</th><th>1</th><th>2</th><th>3</th><th>4</th></tr>”;

/* Use the loop to iterate the rows of the array */
foreach($softwares as $key => $values)
{
    echo “<tr><td>”.$key.“</td>”;

    /* 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 “<td>”.$softwares[$key][$j].“</td>”;
    }
    echo “</tr>”;
 
}
echo “</table>”;
?>

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

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 “<h2>Result</h2>”;

/* Set the heading of the table */
echo “<table border=1><tr><th>ID</th>”;

foreach($marks as $ids)
{
    foreach($ids as $key => $val)
        echo “<th>”.$key.“</th>”;
    break;
}
echo “</tr>”;

/* Use the loop to iterate the rows of the array */
foreach($marks as $key => $values)
{
    echo “<tr><td>”.$key.“</td>”;

    /* Use the loop to iterate the columns of the array */
    foreach($values as $v) {

        /* Read the values of the inner arrays */  
        echo “<td>”.$v.“</td>”;
    }
    echo “</tr>”;
 
}
echo “</table>”;

?>

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

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.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article