how to print multidimensional array in php?

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
Multidimensional array Difference in php
Multidimensional array Difference in php
$members = array (
  array("Vishal",22,18),
  array("Bhavik",15,13),
  array("Shakti",5,2),
  array("Lalit Kumar",17,15)
);

Example





";
echo $members[1][0].": In join: ".$members[1][1].", target: ".$members[1][2].".
"; echo $members[2][0].": In join: ".$members[2][1].", target: ".$members[2][2].".
"; echo $members[3][0].": In join: ".$members[3][1].", target: ".$members[3][2].".
"; ?>

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):





Row number $row

"; echo "
    "; for ($col = 0; $col < 3; $col++) { echo "
  • ".$members[$row][$col]."
  • "; } echo "
"; } ?>

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 "
";
echo "Member ID\tAge";
foreach ( $array as $var ) {
    echo "\n", $var['member_id'], "\t\t", $var['age'];
}

Using array_map

echo "
" ;
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

 $value) {
    foreach ($value as $k => $v) {
        echo "";
        echo ""; // Get index.
        echo ""; // Get value.
        echo "";
    }
}
?>
$k$v

Two dimensional associative array Example

 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

 

Accessing multidimensional array elements

 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.

Leave a Comment