How to Print Multidimensional Arrays in PHP

How to print multidimensional arrays in php

I believe this is your array

$array = Array ( 
0 => Array ( "product_id" => 33 , "amount" => 1 ) ,
1 => Array ( "product_id" => 34 , "amount" => 3 ) ,
2 => Array ( "product_id" => 10 , "amount" => 1 ) );

Using foreach

echo "<pre>";
echo "Product ID\tAmount";
foreach ( $array as $var ) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}

Using array_map

echo "<pre>" ;
echo "Product ID\tAmount";
array_map(function ($var) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}, $array);

Output

Product ID  Amount
33 1
34 3
10 1

Print Multi-dimensional array using loop in PHP

you have associative array inside associative array
You have to loop the first associative array
and inside it loop the associative array
like this

foreach($bazar as $key => $val){

echo $key.'<br>';

foreach($val as $k => $v){
echo $k . ' -> ' .$v . '<br>';
}

}

How to print multidimensional array in php

Here an example for using a foreach:

foreach ($array as $key1 => $level1){
foreach ($level1 as $key2 => $level2){
echo $level2['infos']['id_category'];
}
}

how to echo multidimensional array in php

If you're just debugging:

print_r($your_associative_array);

If you want to print it:

foreach($gplus as $array){
foreach($array as $key=>$value){
echo "Key: $key / Value: $value<br />";
}
}

I want to print multidimensional array in php

There Is Multiple Ways To Do This..

1.

<?php
echo $at[1][1]['a'];
echo $at[1][1]['b'];
echo $at[1][1]['c'];
echo $at[1][2]['a'];
echo $at[1][2]['b'];
echo $at[1][2]['c'];
?>

$ar[1] It's array , $at[1][1] secondOne [1] is index

2.

echo '<pre>'; print_r($ar[1]); echo '<pre/>';

3.

var_dump($ar[1]);

4.

using For

5.

using foreach

How to print a array specific value from multidimensional arrays in php

Solution for your edited input:-

$image_array = array();

foreach ($your_array as $arr){
$image_array[] = array_column($arr['product_option_value'],'product_image');
}

Output:- https://eval.in/657966

PHP: How can I print multi-dimensional array elements using foreach?

The problem is, your using <= instead of <. So your for loop goes one step to far and the array is out of bounds.

for($i = 0; $i < count($shop); $i++){
foreach($shop[$i] as $key => $val)
echo $key . ' = ' . $val . '<br>';
}

Print multidimensional array to table

Here is the code you should try,

foreach ($final_array as $food_array) {
echo '<tr>';
foreach ($food_array as $key1 => $value1) {
echo '<td>' . $value1['label'] . '</td>';
}
echo '<tr>';
}

You need to loop it twice to get your output.

print values from multidimensional associative array in php

This will do the trick:

foreach ($product_info as $key => $val) {

//look for specific key. And do action if needed.
if($key=='product'){

$all = 0;
$all = COUNT($val); //Count lines

//loop lines
for ($x = 0; $x <= $all; $x++) {

//check if line exist
if(isset($val[$x])){

//loop through lines and echo data
foreach ($val[$x] as $c => $d) {

echo $c.' '.$d.'<br>';

}
}
}

}

if($key=='type'){
echo 'This is type: '.$val;
}

}

You should edit it for your needs but this is how you could do it!

Print a key in a PHP multidimensional array

Try with this :

foreach($scoreList as $key => $val)
{
echo $key."</br>";
foreach( $val as $keyItem => $valKey)
{
echo $keyItem ." : ".$valKey."</br>";
}
}

Out put:

Sam
class1 : 76
class2 : 62
class3 : 56
class4 : 60
Matt
class1 : 76
class2 : 62
class3 : 56
class4 : 60
Dave
class1 : 76
class2 : 62
class3 : 56
class4 : 60
Steve
class1 : 76
class2 : 62
class3 : 56
class4 : 60


Related Topics



Leave a reply



Submit