I have this code here which gives me the result I'm looking for, a nicely formatted tree of values.
$todos = $this->db->get('todos'); //store the resulting records
$tree = array(); //empty array for storage
$result = $todos->result_array(); //store results as arrays
foreach ($result as $item){
$id = $item['recordId'];
$parent = $item['actionParent'];
$tree[$id] = isset($tree[$id]) ? $item + $tree[$id] : $item;
$tree[$parent]['_children'][] = &$tree[];
}
echo '<pre>';
print_r($tree);
echo '</pre>';
When I put the code from the foreach into a function like so, I get an empty array. What am I missing?
function adj_tree($tree, $item){
$id = $item['recordId'];
$parent = $item['actionParent'];
$tree[$id] = isset($tree[$id]) ? $item + $tree[$id] : $item;
$tree[$parent]['_children'][] = &$tree[];
}
$todos = $this->db->get('todos'); //store the resulting records
$tree = array(); //empty array for storage
$result = $todos->result_array(); //store results as arrays
foreach ($result as $item){
adj_tree($tree, $item);
}
echo '<pre>';
print_r($tree);
echo '</pre>';