Is there a better method of sorting the array?

Good day, I need to gather the data array of this type
$test = [
 ['title' => 'test1', 'url' => 'test1'],
 ['title' => 'test2', 'url' => 'test2'],
[
 'title' => 'test3',
 'url' => 'test3',
 'children' => [
[
 'title' => 'test4',
 'url' => 'test3/test4',
 'children' => [
[
 'title' => 'test5',
 'url' => 'test3/test4/test5',
]
]
],
]
],
 ['title' => 'test6', 'url' => 'test6'],
 ];

The facts are
$data = [
[
 'id' => 1,
 'parent_id' => 0,
 'name' => 'test1',
 'url' => 'test1',
],
[
 'id' => 2,
 'parent_id' => 0,
 'name' => 'test2',
 'url' => 'test2',
],
[
 'id' => 3,
 'parent_id' => 0,
 'name' => 'test3',
 'url' => 'test3',
],
[
 'id' => 4,
 'parent_id' => 3,
 'name' => 'test4',
 'url' => 'test3/test4',
],
[
 'id' => 5,
 'parent_id' => 4,
 'name' => 'test5',
 'url' => 'test3/test4/test5',
],
[
 'id' => 6,
 'parent_id' => 0,
 'name' => 'test6',
 'url' => 'test6',
],
];


Collect almost I need an array of 2 functions
public function recarray($arr, $data) {
 foreach ($arr as $k => $val) {
 if($k === (int)$data['parent_id']){
 $arr[$k]['children'][(int)$data['id']] = [
 'title' => $data['name'],
 'url' => $data['url']
];
 }elseif (is_array($arr[$k])){
 $ret = $this->recarray($arr[$k], $data);
 if (count($ret)) $arr[$k] = $ret;
}
}
 return $arr;
}

 public function getTestArr(){
 $tree = [];
 foreach ($data as $k=>$v) {
if(@$v['parent_id']){
 $tree = $this->recarray($tree, $v);
}else{
 $tree[$v['id']] = [
 'title' => $v['name'],
 'url' => $v['url'],
];
}
}
 return $tree;
 }


Using my method, the array going, but a bit not in the form puts the keys according to id and I need to go sequentially from 0....n as in the example above, what anyone thought?

here's the result I get
Array
(
 [1] => Array
(
 [title] => test1
 [url] => test1
)

 [2] => Array
(
 [title] => test2
 [url] => test2
)

 [3] => Array
(
 [title] => test3
 [url] => test3
 [children] => Array
(
 [4] => Array
(
 [title] => test4
 [url] => test3/test4
 [children] => Array
(
 [5] => Array
(
 [title] => test5
 [url] => test3/test4/test5
)

)

)

)

)

 [6] => Array
(
 [title] => test6
 [url] => test6
)

)


Of course to solve the problem by using one iterate through the resulting array!!! But that's not the question - but how to avoid this and find a better way
June 10th 19 at 14:39
1 answer
June 10th 19 at 14:41
Solution
Is your string $tree[$v['id']] = [... , use $tree[] = [...
)) what about the children ? they then find parents
just substitute the initial array ===> aim to ensure I've got 1 option - tara.OKon commented on June 10th 19 at 14:44
I just showed how to do a serial array. A turnkey solution is not offered, since I'd originally been doing anything would have cost one-one recursive function, which would be passed the id of the parent. - Roderick92 commented on June 10th 19 at 14:47
so I say - I have for some reason not coming out of the 1st recursive function can you show me? - tara.OKon commented on June 10th 19 at 14:50
I knew that this all will come :)
function rev($arr, $parent_id=0) {
 foreach ($arr as $item) {
 if ($item['parent_id'] != $parent_id)
continue;
 $node = [
 'title' => $item['name'],
 'url' => $item['url'],
];
 $children = rev($arr, $item['id']);
 if (!empty($children))
 $node['children'] = $children;
 $res[] = $node;
}
 if (empty($res))
 return [];
 return $res;
}
$test = rev($data);
print_r($test);
- Roderick92 commented on June 10th 19 at 14:53
to Lesh thanks! - tara.OKon commented on June 10th 19 at 14:56

Find more questions by tags PHPArrays