PHP - SPLessons

PHP Multidimensional Array

Chapter 12

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Multidimensional Array

PHP Multidimensional Array

shape Description

If one-dimensional arrays are extended, PHP Multidimensional Array is obtained which consists of rows and columns and looks like a matrix. The memory location address also adds to the previous element address. They are also referred to as “array of arrays“.

There are 2-D, 3-D, 4-D or N-dimensional arrays. However, it is hard to manage arrays that are more than three levels.

shape Syntax

$array_name[d1][d2][d3]….[dn];

where d1=first dimension
d2=second dimension
.
.
dn=last dimension

Eg:
$a[2][3] : The example denotes that there are 2 rows and 3 columns.A total of 2×3=6 elements of variable.

$b[3][3][3] : The example denotes that there are 3 tables,3 rows and 3 columns. A total of 3x3x3=27 elements of b.

shape Example

<!DOCTYPE html>
<html>
    <body>
        <?php 
            $details = array ( array("Peter","B.Tech",2012), array("John","B.Tech",2010), array("Harry","M.Tech",2013) ); 
            echo "Name : ".$details[0][0]." -> Qualification: ".$details[0][1].", year: ".$details[0][2].".<br/>";
            echo "Name : ".$details[1][0]." -> Qualification: ".$details[1][1].", year: ".$details[1][2].".<br/>";
            echo "Name : ".$details[2][0]." -> Qualification: ".$details[2][1].", year: ".$details[2][2].".<br/>";
        ?>
    </body>
</html>

Output:

Summary

shape Key Points

  • An Array which contains one or more arrays in it called as PHP Multidimensional Array.