<?php
$array = array ( array(
'id' => '2',
'name' => 'Item2',
'children' => array(
array(
'id' => '1',
'name' => 'Item1',
'children' => array(
array('id' => '5', 'name' => 'Item5')
)
),
array(
'id' => '4',
'name' => 'Item4'
)
)
),
array(
'id' => '3',
'name' => 'Item3'
)
);
function list_items($array, $parent = 0) {
$output = array();
foreach ($array as $arr) {
$output[] = array('id' => $arr['id'], 'name' => $arr['name'], 'parent' => $parent);
if (is_array($arr['children'] ?? NULL)) {
$output = array_merge($output, list_items($arr['children'], $arr['id']));
}
}
return $output;
}
$items = list_items($array);
print_r($items);
preferences:
25.07 ms | 405 KiB | 5 Q