最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在php中打印多维数组

SEO心得admin48浏览0评论
本文介绍了如何在php中打印多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我有一个以下格式的数组

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

任何人都可以在这个问题上帮助我.变量的var_dump是.

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

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)

推荐答案

我相信这是您的数组

$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
发布评论

评论列表(0)

  1. 暂无评论