Rumah > pembangunan bahagian belakang > tutorial php > 关于多维数组降维、深度和获取父键的问题

关于多维数组降维、深度和获取父键的问题

WBOY
Lepaskan: 2016-06-23 13:27:29
asal
1297 orang telah melayarinya

我有一个数组,结构是这样的:

$json = '[{"id":27,"children":[{"id":28,"children":[{"id":30},{"id":29}]}]},{"id":31},{"id":32}]';$arry = json_decode($json,true);
Salin selepas log masuk

也有可能是这样的结构:
$json = '[{"id":27,"children":[{"id":30,"children":[{"id":31}]}]},{"id":28,"children":[{"id":29}]},{"id":32}]';$arry = json_decode($json,true);
Salin selepas log masuk


也就是说层数、顺序都不是固定的。
我想把他降为一维数组,并记录下每一个一维数组的深度,对应的父键等

以下是第二个数组期望得到的结果:
Array ([0] => Array ( [id] => 27 [level] => 0 [parent] => 0 )[1] => Array ( [id] => 30 [level] => 1 [parent] => 27 )[2] => Array ( [id] => 31 [level] => 2 [parent] => 30 )[3] => Array ( [id] => 28 [level] => 0 [parent] => 0 )[4] => Array ( [id] => 29 [level] => 1 [parent] => 28 )[5] => Array ( [id] => 32 [level] => 0 [parent] => 0 ) )
Salin selepas log masuk


想了一晚上了。。。请大神帮帮忙


回复讨论(解决方案)

$json = '[{"id":27,"children":[{"id":30,"children":[{"id":31}]}]},{"id":28,"children":[{"id":29}]},{"id":32}]';$arry = json_decode($json,true);print_r(foo($arry));function foo($ar, $level=0, $parent=0, &$res=array()) {  foreach($ar as $v) {    $t = array();    $v['level'] = $level;    $v['parent'] = $parent;    if(isset($v['children'])) {      $t = $v['children'];      unset($v['children']);    }    $res[] = $v;    if($t) foo($t, $level+1, $v['id'], $res);  }  return $res;}
Salin selepas log masuk
Salin selepas log masuk
Array(    [0] => Array        (            [id] => 27            [level] => 0            [parent] => 0        )    [1] => Array        (            [id] => 30            [level] => 1            [parent] => 27        )    [2] => Array        (            [id] => 31            [level] => 2            [parent] => 30        )    [3] => Array        (            [id] => 28            [level] => 0            [parent] => 0        )    [4] => Array        (            [id] => 29            [level] => 1            [parent] => 28        )    [5] => Array        (            [id] => 32            [level] => 0            [parent] => 0        ))
Salin selepas log masuk
Salin selepas log masuk

$json = '[{"id":27,"children":[{"id":30,"children":[{"id":31}]}]},{"id":28,"children":[{"id":29}]},{"id":32}]';$arry = json_decode($json,true);print_r(foo($arry));function foo($ar, $level=0, $parent=0, &$res=array()) {  foreach($ar as $v) {    $t = array();    $v['level'] = $level;    $v['parent'] = $parent;    if(isset($v['children'])) {      $t = $v['children'];      unset($v['children']);    }    $res[] = $v;    if($t) foo($t, $level+1, $v['id'], $res);  }  return $res;}
Salin selepas log masuk
Salin selepas log masuk
Array(    [0] => Array        (            [id] => 27            [level] => 0            [parent] => 0        )    [1] => Array        (            [id] => 30            [level] => 1            [parent] => 27        )    [2] => Array        (            [id] => 31            [level] => 2            [parent] => 30        )    [3] => Array        (            [id] => 28            [level] => 0            [parent] => 0        )    [4] => Array        (            [id] => 29            [level] => 1            [parent] => 28        )    [5] => Array        (            [id] => 32            [level] => 0            [parent] => 0        ))
Salin selepas log masuk
Salin selepas log masuk




啊!!!!
十分感谢!
    if(isset($v['children'])) {      $t = $v['children'];      unset($v['children']);    }
Salin selepas log masuk

这一步我实在想不出来。

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan