PHP Multidimensional Arrays

PHP  Multidimensional Arrays

PHP Multidimensional Arrays values are stored using more than one key unlike other arrays that use only one key.

PHP Multidimensional Arrays

PHP  Multidimensional Arrays are special type of arrays that have  atleast one array or more inside them i.e A two dimensional array  needs  atleast two keys to select  element inside the array whereas three dimensional array needs three keys.

Two dimensional Arrays

Two dimensional arrays are array  of arrays

See below Table:

PHP Multidimensional Arrays
PHP Multidimensional Arrays

 

See also:PHP  Arrays

These values can be stored in this type of arrays as show in below code.


$marks= array
(
array("Eric",22,18),
array("John",15,13),
array("Mellisa",5,2)
);

Above marks array has two indices(row and column)

Example below shows how we can get access of these values.

<?php

echo $marks[0][0].": In stock: ".$marks[0][1].", sold: ".$marks[0][2].".<br>";

echo $marks[1][0].": In stock: ".$marks[1][1].", sold: ".$marks[1][2].".<br>";

echo $marks[2][0].": In stock: ".$marks[2][1].", sold: ".$marks[2][2].".<br>";

?>

A for loop can be used to print all the values of this array.See Below Example

$marks= array
(
array("Eric",22,18),
array("John",15,13),
array("Mellisa",5,2)
);
<?php

for ($row = 0; $row < 3; $row++) {

echo "<p><b>Row number is $row</b></p>";

echo "<ul>";

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

echo "<li>".$marks[$row][$col]."</li>";

}

echo "</ul>";

}

?>

 

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com