Function to generate array format:
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = buildTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
$tree = buildTree($rows);
Array generated:
Array
(
[0] => Array
(
[TASK_ID] => 2
[PARENT_TASKID] => 0
[TASK_LEVEL] => 0
[children] => Array
(
[0] => Array
(
[TASK_ID] => 9
[PARENT_TASKID] => 2
[TASK_LEVEL] => 1
)
[1] => Array
(
[TASK_ID] => 10
[PARENT_TASKID] => 2
[TASK_LEVEL] => 1
)
)
)
[1] => Array
(
[TASK_ID] => 1
[PARENT_TASKID] => 0
[TASK_LEVEL] => 0
[children] => Array
(
[0] => Array
(
[TASK_ID] => 4
[PARENT_TASKID] => 1
[TASK_LEVEL] => 1
)
[1] => Array
(
[TASK_ID] => 5
[PARENT_TASKID] => 1
[TASK_LEVEL] => 1
[children] => Array
(
[0] => Array
(
[TASK_ID] => 6
[PARENT_TASKID] => 5
[TASK_LEVEL] => 2
)
)
)
)
)
)
How can I change the above format into below:
Array
(
[0] => Array
(
[TASK_ID] => 2
[PARENT_TASKID] => 0
)
[1] => Array
(
[TASK_ID] => 9
[PARENT_TASKID] => 2
)
[2] => Array
(
[TASK_ID] => 10
[PARENT_TASKID] => 2
)
)
Tried below code:
$flat = call_user_func_array('array_merge', $array);
and
$array = your array
$result = call_user_func_array('array_merge', $array);
echo "<pre>";
print_r($result);
But does not seem to work. Please Help.
Thanks.