本文介绍了循环多维PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我很难遍历PHP多维数组,我想知道循环数组的最佳方法.这是我要遍历的当前数组.
Hello I am having hard time looping through the PHP multidimensional array, I want to know the best possible way of looping an array. This is the current array that I am trying to loop through.
Array
(
[bathroom] => Array
(
[name] => Bathroom
[things] => Array
(
[0] => Array
(
[name] => Cheval Mirrow
[cubic] => .14
[quantity] => 1
)
[1] => Array
(
[name] => Carton/Wine
[cubic] => .07
[quantity] => 1
)
[2] => Array
(
[name] => Carton/picture
[cubic] => .07
[quantity] => 1
)
)
)
)
我已经尝试过此代码
$keys = array_keys($array);
for($i = 0; $i < count($array); $i++) {
echo $keys[$i] . "<br>";
foreach($array[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
foreach($array[$value[$i]] as $key1 => $value1){
echo $key1.":". $value1."<br>";
}
}
echo "<br>";
}
我现在能够获得第一个值,问题是我无法获得事物数组的值,对此我出错了,有人可以告诉我我在哪里出错了.
I am able to get the first value now the issues is that I am not able to get the values of things array, I am getting error on this, can someone tell me where I am getting wrong on this.
推荐答案
以下是如何处理数组的示例:
Here's an example of how you can process your array:
foreach ($array as $key => $value) {
echo "$key:<br>\n";
echo " name: {$value['name']}<br>\n";
foreach ($value['things'] as $t => $thing) {
echo "\tthing $t:<br>\n";
foreach ($thing as $name => $val) {
echo "\t $name: $val<br>\n";
}
}
}
输出:
bathroom:<br>
name: Bathroom<br>
thing 0:<br>
name: ChevalMirrow<br>
cubic: 0.14<br>
quantity: 1<br>
thing 1:<br>
name: Carton/Wine<br>
cubic: 0.07<br>
quantity: 1<br>
thing 2:<br>
name: Carton/picture<br>
cubic: 0.07<br>
quantity: 1<br>
这篇关于循环多维PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!