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
(
)
)
)
9 个解决方案
#1
14
It looks like you're only trying to write one important value from each array. Try a recursive function like so:
看起来你只是想从每个数组中写一个重要的值。尝试像这样的递归函数:
function RecursiveWrite($array) {
foreach ($array as $vals) {
echo $vals['comment_content'] . "\n";
RecursiveWrite($vals['child']);
}
}
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).
您还可以使它更具动态性,并将'comment_content'和'child'字符串作为参数传递给函数(并在递归调用中继续传递它们)。
#2
28
<pre>
<?php print_r ($array); ?>
</pre>
#3
5
Proper, Better, and Clean Solution:
正确,更好,更清洁的解决方案:
function traverseArray($array)
{
// Loops through each element. If element again is array, function is recalled. If not, result is echoed.
foreach ($array as $key => $value)
{
if (is_array($value))
{
Self::traverseArray($value); // Or
// traverseArray($value);
}
else
{
echo $key . " = " . $value . "<br />\n";
}
}
}
You simply call in this helper function traverseArray($array)
in your current/main class like this:
您只需在当前/主类中调用此辅助函数traverseArray($ array),如下所示:
$this->traverseArray($dataArray); // Or
// traverseArray($dataArray);
source: http://snipplr.com/view/10200/recursively-traverse-a-multidimensional-array/
来源:http://snipplr.com/view/10200/recursively-traverse-a-multidimensional-array/
#4
2
print_r($arr)
usually gives pretty readable result.
print_r($ arr)通常给出非常可读的结果。
#5
2
if you wanted to store it as a variable you could do:
如果你想将它存储为变量,你可以这样做:
recurse_array($values){
$content = '';
if( is_array($values) ){
foreach($values as $key => $value){
if( is_array($value) ){
$content.="$key<br />".recurse_array($value);
}else{
$content.="$key = $value<br />";
}
}
}
return $content;
}
$array_text = recurse_array($array);
Obviously you can format as needed!
显然你可以根据需要格式化!
#7
0
If you're outputting the data for debugging and development purposes, Krumo is great for producing easily readable output. Check out the example output.
如果您正在输出数据以进行调试和开发,那么Krumo非常适合生成易读的输出。查看示例输出。
#8
0
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/
递归通常是你的答案,但另一种方法是使用引用。见http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/
#9
0
There are multiple ways to do that
有多种方法可以做到这一点
1) - print_r($array);
or if you want nicely formatted array then
1) - print_r($ array);或者如果你想要格式良好的数组那么
echo '<pre>'; print_r($array); echo '<pre/>';
//-------------------------------------------------
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.
// ------------------------------------------------ - 2) - 使用var_dump($ array)获取数组内容的更多信息,如数据类型和长度。 // ------------------------------------------------ - 3) - 你可以使用php的foreach()循环数组;并获得所需的输出。
function recursiveFunction($array) {
foreach ($array as $val) {
echo $val['comment_content'] . "\n";
recursiveFunction($vals['child']);
}
}
#1
14
It looks like you're only trying to write one important value from each array. Try a recursive function like so:
看起来你只是想从每个数组中写一个重要的值。尝试像这样的递归函数:
function RecursiveWrite($array) {
foreach ($array as $vals) {
echo $vals['comment_content'] . "\n";
RecursiveWrite($vals['child']);
}
}
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).
您还可以使它更具动态性,并将'comment_content'和'child'字符串作为参数传递给函数(并在递归调用中继续传递它们)。
#2
28
<pre>
<?php print_r ($array); ?>
</pre>
#3
5
Proper, Better, and Clean Solution:
正确,更好,更清洁的解决方案:
function traverseArray($array)
{
// Loops through each element. If element again is array, function is recalled. If not, result is echoed.
foreach ($array as $key => $value)
{
if (is_array($value))
{
Self::traverseArray($value); // Or
// traverseArray($value);
}
else
{
echo $key . " = " . $value . "<br />\n";
}
}
}
You simply call in this helper function traverseArray($array)
in your current/main class like this:
您只需在当前/主类中调用此辅助函数traverseArray($ array),如下所示:
$this->traverseArray($dataArray); // Or
// traverseArray($dataArray);
source: http://snipplr.com/view/10200/recursively-traverse-a-multidimensional-array/
来源:http://snipplr.com/view/10200/recursively-traverse-a-multidimensional-array/
#4
2
print_r($arr)
usually gives pretty readable result.
print_r($ arr)通常给出非常可读的结果。
#5
2
if you wanted to store it as a variable you could do:
如果你想将它存储为变量,你可以这样做:
recurse_array($values){
$content = '';
if( is_array($values) ){
foreach($values as $key => $value){
if( is_array($value) ){
$content.="$key<br />".recurse_array($value);
}else{
$content.="$key = $value<br />";
}
}
}
return $content;
}
$array_text = recurse_array($array);
Obviously you can format as needed!
显然你可以根据需要格式化!
#6
#7
0
If you're outputting the data for debugging and development purposes, Krumo is great for producing easily readable output. Check out the example output.
如果您正在输出数据以进行调试和开发,那么Krumo非常适合生成易读的输出。查看示例输出。
#8
0
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/
递归通常是你的答案,但另一种方法是使用引用。见http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/
#9
0
There are multiple ways to do that
有多种方法可以做到这一点
1) - print_r($array);
or if you want nicely formatted array then
1) - print_r($ array);或者如果你想要格式良好的数组那么
echo '<pre>'; print_r($array); echo '<pre/>';
//-------------------------------------------------
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.
// ------------------------------------------------ - 2) - 使用var_dump($ array)获取数组内容的更多信息,如数据类型和长度。 // ------------------------------------------------ - 3) - 你可以使用php的foreach()循环数组;并获得所需的输出。
function recursiveFunction($array) {
foreach ($array as $val) {
echo $val['comment_content'] . "\n";
recursiveFunction($vals['child']);
}
}