0
我有一個數組(數據來自鄰接表),它看起來像:獲取路徑列
$data = Array
(
[0] => Array
(
[id] => 1
[name] => Anniversary
[parent] => 0
)
[1] => Array
(
[id] => 12
[name] => New arrives
[parent] => 1
)
[2] => Array
(
[id] => 13
[name] => Discount
[parent] => 12
)
[3] => Array
(
[id] => 6
[name] => Birthday
[parent] => 0
)
)
我有通過ID返回路徑數組的函數:
function categoryRecursion($element, $input) {
$return = array_filter($input, function($v) use ($element) { return $v->id == $element; });
$last = end($return);
$str[] = $last;
if($last->parent !=0) {
$str[] = categoryRecursion($last->parent, $input);
}
return array_reverse($str); //implode('->',array_reverse($str));
}
當我使用它:
print(categoryRecursion('13',$data));
我得到這樣的輸出:
Array
(
[0] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Anniversary
[parent] => 0
)
)
[1] => stdClass Object
(
[id] => 12
[name] => New arrives
[parent] => 1
)
)
[1] => stdClass Object
(
[id] => 13
[name] => Discount
[parent] => 12
)
)
但是我正在尋找的方式來獲得輸出:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Anniversary
[parent] => 0
)
)
[1] => stdClass Object
(
[id] => 12
[name] => New arrives
[parent] => 1
)
)
[2] => stdClass Object
(
[id] => 13
[name] => Discount
[parent] => 12
)
)
如何解決我的功能?