[snip] …stuff…. [/snip] For those interested here is where the problem seems to occur - $child = array ( 'id' => $id, 'parentId' => $pid, 'children' => array() ); $children[$id][] = &$child; foreach( $parents[$pid] as &$items ){ foreach( $items as &$item ){ $item['children'][$id] = &$child; // error: Cannot use string offset as an array } } Maybe someone will see this and know what's going on. Before the foreach $children is an array.