I want to use this Recursive Function to convert a php array into an array of parent and childs.
I have a this Cell:
class MenuCell extends Cell {
public function display($menu) {
$this->loadModel('Menus');
$menus = $this -> Menus -> find('all', [
'contain' => ['MenuItems']
]) -> where(['id' => $menu]);
$menus = $this -> buildTree($menus);
$this -> set(compact('menus'));
$this -> set('_serialize', ['menus']);
}
public function buildTree($ar, $pid = null) {
$op = array();
foreach($ar as $item) {
if ($item['parentId'] == $pid) {
$op[$item['id']] = array(
'name' => $item['name'],
'parentId' => $item['parentId']
);
// using recursion
$children = $this ->buildTree($ar, $item['id']);
if ($children) {
$op[$item['id']]['children'] = $children;
}
}
}
return $op;
}
}
However if i do the samething in controller, It works very fine. Any help would save my day.

find('threaded')?