php 如何在php中打印多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12651864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:58:24  来源:igfitidea点击:

How to print multidimensional arrays in php

php

提问by Damith

Possible Duplicate:
php - How do I print this multidimensional array?

可能重复:
php - 如何打印这个多维数组?

I have an array in below format

我有以下格式的数组

Array ( [0] => Array ( [product_id] => 33 [amount] => 1 ) [1] => Array ( [product_id] => 34 [amount] => 3 ) [2] => Array ( [product_id] => 10 [amount] => 1 ) ) 

I want to get output from that array as below format

我想从该数组中获得如下格式的输出

Product ID    Amount
33             1
34             3
10             1

Can anyone please help me regarding this problem. var_dump of the variable is.

任何人都可以帮助我解决这个问题。var_dump 的变量是。

array
  0 => 
    array
      'product_id' => string '33' (length=2)
      'amount' => string '1' (length=1)
  1 => 
    array
      'product_id' => string '34' (length=2)
      'amount' => string '3' (length=1)
  2 => 
    array
      'product_id' => string '10' (length=2)
      'amount' => string '1' (length=1)

回答by Baba

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

使用 foreach

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

Using array_map

使用 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

回答by Hearaman

     <table>
        <tr>
            <th>Product Id</th>
            <th>Ammount</th>
        </tr>

        <?php
        foreach ($yourArray as $subAray)
        {
            ?>
            <tr>
                <td><?php echo $subAray['product_id']; ?></td>
                <td><?php echo $subAray['amount']; ?></td>
            </tr>
            <?php
        }
        ?>
    </table>

回答by Ashwini Agarwal

Try this..

尝试这个..

foreach($arr as $a)
{
    echo $a['product_id'];
    echo $a['amount'];
}

Format as per your requirment.

根据您的要求格式化。