在 PHP 中回显多维数组
我有一个多维数组,我试图找出如何简单地“回显”数组的元素。数组的深度未知,因此可以嵌套得很深。
对于下面的数组,正确的 echo 顺序是:
This is a parent comment
This is a child comment
This is the 2nd child comment
This is another parent comment
这是我正在讨论的数组:
Array
(
[0] => Array
(
[comment_id] => 1
[comment_content] => This is a parent comment
[child] => Array
(
[0] => Array
(
[comment_id] => 3
[comment_content] => This is a child comment
[child] => Array
(
[0] => Array
(
[comment_id] => 4
[comment_content] => This is the 2nd child comment
[child] => Array
(
)
)
)
)
)
)
[1] => Array
(
[comment_id] => 2
[comment_content] => This is another parent comment
[child] => Array
(
)
)
)
I have a multidimensional array and I'm trying to find out how to simply "echo" the elements of the array. The depth of the array is not known, so it could be deeply nested.
In the case of the array below, the right order to echo would be:
This is a parent comment
This is a child comment
This is the 2nd child comment
This is another parent comment
This is the array I was talking about:
Array
(
[0] => Array
(
[comment_id] => 1
[comment_content] => This is a parent comment
[child] => Array
(
[0] => Array
(
[comment_id] => 3
[comment_content] => This is a child comment
[child] => Array
(
[0] => Array
(
[comment_id] => 4
[comment_content] => This is the 2nd child comment
[child] => Array
(
)
)
)
)
)
)
[1] => Array
(
[comment_id] => 2
[comment_content] => This is another parent comment
[child] => Array
(
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
看起来您只是想从每个数组中写入一个重要值。尝试像这样的递归函数:
您还可以使其更加动态一点,并将
'comment_content'
和'child'
字符串作为参数传递到函数中(然后继续在递归调用中传递它们)。It looks like you're only trying to write one important value from each array. Try a recursive function like so:
You could also make it a little more dynamic and have the
'comment_content'
and'child'
strings passed into the function as parameters (and continue passing them in the recursive call).正确、更好、干净的解决方案:
您只需在当前/主类中调用此辅助函数
traverseArray($array)
即可,如下所示:来源:http://snipplr.com/view/10200/recursively-traverse-a-multiDimension-array/
Proper, Better, and Clean Solution:
You simply call in this helper function
traverseArray($array)
in your current/main class like this:source: http://snipplr.com/view/10200/recursively-traverse-a-multidimensional-array/
print_r($arr)
通常会给出非常可读的结果。print_r($arr)
usually gives pretty readable result.如果你想将它存储为变量,你可以这样做:
显然你可以根据需要格式化!
if you wanted to store it as a variable you could do:
Obviously you can format as needed!
有多种方法可以做到这一点
1) -
print_r($array);
或者如果您想要格式良好的数组,则//---------- ---------------------------------------
2) - 使用 var_dump($array) 获取数组内容的更多信息,例如数据类型和长度。
//------------------------------------------------ -
3) - 您可以使用 php 的 foreach(); 循环数组并获得所需的输出。
There are multiple ways to do that
1) -
print_r($array);
or if you want nicely formatted array then//-------------------------------------------------
2) - use
var_dump($array)
to get more information of the content in the array like datatype and length.//-------------------------------------------------
3) - you can loop the array using php's
foreach();
and get the desired output.尝试使用 var_dump 函数。
Try to use var_dump function.
如果您输出数据用于调试和开发目的,Krumo 非常适合生成易于阅读的输出。查看示例输出。
If you're outputting the data for debugging and development purposes, Krumo is great for producing easily readable output. Check out the example output.
递归通常是您的答案,但另一种选择是使用引用。请参阅 http://www.ideashower .com/our_solutions/create-a-parent-child-array-struct-in-one-pass/
Recursion would be your answer typically, but an alternative would be to use references. See http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/