我正在通过ajax帖子接收以下多维数组:
array(1) {
["result"] => array(3) {
[0] => array(2) {
[0] => string(1)
"0" [1] => array(2) {
["id"] => string(5)
"1§10" ["children"] => array(2) {
[0] => array(1) {
["id"] => string(6)
"1§3$0"
} [1] => array(1) {
["id"] => string(6)
"1§1$0"
}
}
}
} [1] => array(2) {
[0] => string(1)
"1" [1] => array(1) {
["id"] => string(5)
"3§20"
}
} [2] => array(2) {
[0] => string(1)
"2" [1] => array(2) {
["id"] => string(5)
"2§30" ["children"] => array(1) {
[0] => array(1) {
["id"] => string(6)
"2§2$0"
}
}
}
}
}
}
有没有一种方法只能用php逐个返回字符串?数组中字符串的深度并不重要,因为我只会解析并分解id字符串。
答案 0 :(得分:0)
您可以通过以下方式实现简单的回复功能:
function printString($arg) {
if (is_string($arg))
echo $arg . PHP_EOL;
else (is_array($arg)) {
foreach($arg as $e)
printString($e);
}
}
或将array_walk_recursive
用作:
function printStr($item, $key) {
echo $item . PHP_EOL;
}
array_walk_recursive($arr, 'printStr');
如果您只需要“ id”字符串,请执行以下操作:
function printId($item, $key) { if ($key == "id") echo $item . PHP_EOL; }
array_walk_recursive($arr, 'printId');