Today, We want to share with you how to print multidimensional array in php.In this post we will show you get value from multidimensional array php, hear for for loop in php we will give you demo and example for implement.In this post, we will learn about Multidimensional Arrays In Laravel with an example.
PHP Multidimensional Arrays
Name | Join | Sold |
---|---|---|
Vishal | 22 | 18 |
Bhavik | 15 | 13 |
Shakti | 5 | 2 |
Lalit Kumar | 17 | 15 |
$members = array ( array("Vishal",22,18), array("Bhavik",15,13), array("Shakti",5,2), array("Lalit Kumar",17,15) );
Example
<!DOCTYPE html> <html> <body> <?php $members = array ( array("Vishal",22,18), array("Bhavik",15,13), array("Shakti",5,2), array("Lalit Kumar",17,15) ); echo $members[0][0].": In join: ".$members[0][1].", target: ".$members[0][2].".<br>"; echo $members[1][0].": In join: ".$members[1][1].", target: ".$members[1][2].".<br>"; echo $members[2][0].": In join: ".$members[2][1].", target: ".$members[2][2].".<br>"; echo $members[3][0].": In join: ".$members[3][1].", target: ".$members[3][2].".<br>"; ?> </body> </html>
Results:
Vishal: In join: 22, target: 18. Bhavik: In join: 15, target: 13. Shakti: In join: 5, target: 2. Lalit Kumar: In join: 17, target: 15.
i can also update a for php loop inside another for loop to get the data elements of the $members array (i still have to point to the two indices):
<!DOCTYPE html> <html> <body> <?php $members = array ( array("Vishal",22,18), array("Bhavik",15,13), array("Shakti",5,2), array("Lalit Kumar",17,15) ); for ($row = 0; $row < 4; $row++) { echo "<p><b>Row number $row</b></p>"; echo "<ul>"; for ($col = 0; $col < 3; $col++) { echo "<li>".$members[$row][$col]."</li>"; } echo "</ul>"; } ?> </body> </html>
Results
Row number 0 Vishal 22 18 Row number 1 Bhavik 15 13 Row number 2 Shakti 5 2 Row number 3 Lalit Kumar 17 15
How to print multidimensional arrays in php?
sample Array
$array = Array ( 0 => Array ( "member_id" => 33 , "age" => 1 ) , 1 => Array ( "member_id" => 34 , "age" => 3 ) , 2 => Array ( "member_id" => 10 , "age" => 1 ) );
Using foreach
echo "<pre>"; echo "Member ID\tAge"; foreach ( $array as $var ) { echo "\n", $var['member_id'], "\t\t", $var['age']; }
Using array_map
echo "<pre>" ; echo "Member ID\tAge"; array_map(function ($var) { echo "\n", $var['member_id'], "\t\t", $var['age']; }, $array);
results:
Member ID Age 33 1 34 3 10 1
Output a Multidimensional Array with index and value and print into the table
Example
Array ( [0] => Array ( [id] => 11 [member_id] => 7 [name] => Lenova g580 [description] => Lenova g580 [price] => 18.00 [virtual] => 1 [active] => 1 [sort_order] => 11 [created] => 2021-11-25 14:08:03 [modified] => 2021-11-25 14:08:03 [image] => NONE ) [1] => Array ( [id] => 19 [member_id] => 7 [name] => Lenova z570 [description] => Lenova z570 [price] => 1.00 [virtual] => 1 [active] => 1 [sort_order] => 19 [created] => 2021-11-25 14:10:02 [modified] => 2021-11-25 14:10:02 [image] => NONE ) )
Output Multidimensional Array with index and value in table
<table> <?php foreach ($members as $key => $value) { foreach ($value as $k => $v) { echo "<tr>"; echo "<td>$k</td>"; // Get index. echo "<td>$v</td>"; // Get value. echo "</tr>"; } } ?> </table>
Two dimensional associative array Example
<?php // PHP program to creating two // dimensional associative array $points = array( // Rakesh will act as key "Rakesh" => array( // Unit and points are // the key value pair "BOL" => 95, "BAT" => 85, "ALL" => 74, ), // Dhaval will act as key "Dhaval" => array( // Unit and points are // the key value pair "BOL" => 78, "BAT" => 98, "ALL" => 46, ), // Anjali will act as key "Anjali" => array( // Unit and points are // the key value pair "BOL" => 88, "BAT" => 46, "ALL" => 99, ), ); echo "Display Points: \n"; print_r($points); ?>
Three Dimensional Array
<?php // PHP program to creating three // dimensional array // Create three nested array $myarray = array( array( array(1, 2), array(3, 4), ), array( array(5, 6), array(7, 8), ), ); // Display the array information print_r($myarray); ?>
Accessing multidimensional array elements
<?php // PHP code to create // multidimensional array // Creating multidimensional // associative array $points = array( // Rakesh will act as key "Rakesh" => array( // Unit and points are // the key value pair "BOL" => 95, "BAT" => 85, "ALL" => 74, ), // Dhaval will act as key "Dhaval" => array( // Unit and points are // the key value pair "BOL" => 78, "BAT" => 98, "ALL" => 46, ), // Anjali will act as key "Anjali" => array( // Unit and points are // the key value pair "BOL" => 88, "BAT" => 46, "ALL" => 99, ), ); // Rakesh in BOL subject echo $points['Rakesh']['BOL'] . "\n"; foreach($points as $point) { echo $point['BOL']. " ".$point['BAT']." ".$point['ALL']."\n"; } ?>
I hope you get an idea about how to print multidimensional array in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donβt forget to share.