如何以高级方式打印多维数组?

问题描述 投票:-2回答:3

以下是代码

print_r($_POST).'<br><br>';

foreach($_POST as $key => $value){
if(is_array($value))
{
    foreach($value as $key => $value)
    {
    echo "<br><br>".$key." ".$value."<br>";
    }
}
echo "<br>";
}

输出是

数组([Cake_on_Name] =>数组([0] => Rachana [1] => Sarika [2] => Pratik)[Cake_on_Date] =>数组([0] => 2017-12-19 [1] => 2017-12-19 [2] => 2017-12-19)[Cake_on_Time] =>数组([0] => 10 AM [1] => 10 AM [2] => 10 AM)[Cake_Receiver_Name] =>数组([0] => [1] => [2] => Ashvin Ade)[Cake_on_Address] =>数组([0] => [1] => [2] => Amravati)[pincode] =>数组([0] => 444607 [1] => 444607 [2] => 444607)[cheu] => 1-1 @ 500,54-1 @ 1000,65-1 @ 1000,54-1 @ 300,56 -1 @ 1000,65-1 @ 300,54-1 @ 1000 [clientinput] =>添加细节和购买)

0组成

1排

2实用

0 2017-12-19

1 2017-12-19

2 2017-12-19

0 10 A.M.

1 10 A.M.

2 10 A.M.

0

1

2 Ashwin Adar

0

1

2 Amravati

0 444607

1 444607

2 444607

但我希望输出为:

0组成

0 2017-12-19

0 10 A.M.

0

0

0 444607

1排

1 2017-12-19

1 10 A.M.

1

1

1 444607

2实用

2 2017-12-19

2 10 A.M.

2 Ashwin Adar

2 Amravati

2 444607

php arrays
3个回答
0
投票

你可以试试这个。

$length = count($_POST["Cake_on_Name"]);
for($i=0; $i<=$length;$i++) {

foreach($_POST as $key => $value)
{
echo $i ." ". $value[$i]."<br>";
}

}

0
投票

你把它放在<pre>标签。

echo '<pre>' . print_r( $_POST, 1 ) . '</pre>';

这只在html中显示这种方式。从命令行或如果文档格式为纯文本,它已打印出换行符。从网页输出,您还可以:

//this will also show all other content as standard text with line breaks.
header('Content-Type: text/plain');

enter image description here


0
投票

你可以使用array_map并迭代结果。

$zipped = array_map(null, ...array_values($_POST));

foreach ($zipped as $item) {
    foreach ($item as $index => $value) {
        echo $index . ' ' . $value;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.