7

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

我有一个以下格式的数组

数组 ( [0] => 数组 ( [product_id] => 33 [amount] => 1 ) [1] => 数组 ( [product_id] => 34 [amount] => 3 ) [2] => 数组 ( [ product_id] => 10 [数量] => 1))

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

产品编号金额
33 1
34 3
10 1

谁能帮我解决这个问题。var_dump 的变量是。

大批
  0 =>
    大批
      'product_id' => 字符串'33'(长度=2)
      '数量' => 字符串'1'(长度=1)
  1 =>
    大批
      'product_id' => 字符串'34'(长度=2)
      '数量' => 字符串'3'(长度=1)
  2 =>
    大批
      'product_id' => 字符串'10'(长度=2)
      '数量' => 字符串'1'(长度=1)

4

3 回答 3

8

我相信这是你的阵列

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

使用foreach

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

使用array_map

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

输出

Product ID  Amount
33          1
34          3
10          1
于 2012-09-29T10:23:17.370 回答
3
     <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>
于 2012-09-29T10:26:59.737 回答
2

尝试这个..

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

根据您的要求格式化。

于 2012-09-29T10:22:51.473 回答