在PHP中回显多维数组
发布时间:2020-12-13 16:25:58 所属栏目:PHP教程 来源:网络整理
导读:我有一个多维数组,我试图找出如何简单地“回显”数组的元素.阵列的深度是不知道的,所以它可以是深层次的. 在下面的数组的情况下,正确的回音顺序是: This is a parent commentThis is a child commentThis is the 2nd child commentThis is another parent c
我有一个多维数组,我试图找出如何简单地“回显”数组的元素.阵列的深度是不知道的,所以它可以是深层次的.
在下面的数组的情况下,正确的回音顺序是: 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 ( ) ) )
看起来你只是想从每个数组中写出一个重要的值.尝试像这样的递归函数:
function RecursiveWrite($array) { foreach ($array as $vals) { echo $vals['comment_content'] . "n"; RecursiveWrite($vals['child']); } } 您还可以使其更加动态,并将’comment_content’和’child’字符串作为参数传递到函数中(并在递归调用中继续传递). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |