Как создать массив из этого набора результатов (вложенные категории, хранящиеся в базе данных с моделью обхода)?

Исходя из этого вопроса: Получение измененной модели обхода дерева предзаказов (вложенный набор) в <ul>

Следующая логика используется для построения упорядоченного списка, но как сделать то же самое с массивом?

Я хочу построить вложенный массив.

// bootstrap loop $result = ''; $currDepth = -1; // -1 to get the outer <ul> while (!empty($tree)) { $currNode = array_shift($tree); // Level down? if ($currNode['depth'] > $currDepth) { // Yes, open <ul> $result .= '<ul>'; } // Level up? if ($currNode['depth'] < $currDepth) { // Yes, close n open <ul> $result .= str_repeat('</ul>', $currDepth - $currNode['depth']); } // Always add node $result .= '<li>' . $currNode['title'] . '</li>'; // Adjust current depth $currDepth = $currNode['depth']; // Are we finished? if (empty($tree)) { // Yes, close n open <ul> $result .= str_repeat('</ul>', $currDepth + 1); } } print $result; 

Ах, наконец-то, для чего ссылки удобны:

 <?php $tree = array( array('Cat 1', 'depth' => 0), array('Cat 2', 'depth' => 1), array('Cat 3', 'depth' => 1), array('Cat 4', 'depth' => 2), array('Cat 5', 'depth' => 1), array('Cat 6', 'depth' => 2), array('Cat 7', 'depth' => 3), array('Cat 8', 'depth' => 1) ); //same as before $currDepth = -1; //initilialize result $result = array(); //create path structure for depths $path = array(); //create 'root' node $olditem = array('children'=> &$result); foreach($tree as $item){ if($item['depth'] > $currDepth){ //remove possible old reference (old depth of other branch if(isset($path[$item['depth']])) unset($path[$item['depth']]); //make sure we have an array entry if(!isset($olditem['children'])) $olditem['children'] = array(); //acquire target $path[$item['depth']] = &$olditem['children']; } if($item['depth'] != $currDepth) unset($olditem); //set correct target $currDepth = $item['depth']; //add item $path[$currDepth][] = &$item; //copy & remove reference $olditem = &$item; unset($item); } //always nice to clean up reference bombs: unset($path); unset($olditem); var_dump($result); ?> с <?php $tree = array( array('Cat 1', 'depth' => 0), array('Cat 2', 'depth' => 1), array('Cat 3', 'depth' => 1), array('Cat 4', 'depth' => 2), array('Cat 5', 'depth' => 1), array('Cat 6', 'depth' => 2), array('Cat 7', 'depth' => 3), array('Cat 8', 'depth' => 1) ); //same as before $currDepth = -1; //initilialize result $result = array(); //create path structure for depths $path = array(); //create 'root' node $olditem = array('children'=> &$result); foreach($tree as $item){ if($item['depth'] > $currDepth){ //remove possible old reference (old depth of other branch if(isset($path[$item['depth']])) unset($path[$item['depth']]); //make sure we have an array entry if(!isset($olditem['children'])) $olditem['children'] = array(); //acquire target $path[$item['depth']] = &$olditem['children']; } if($item['depth'] != $currDepth) unset($olditem); //set correct target $currDepth = $item['depth']; //add item $path[$currDepth][] = &$item; //copy & remove reference $olditem = &$item; unset($item); } //always nice to clean up reference bombs: unset($path); unset($olditem); var_dump($result); ?> с <?php $tree = array( array('Cat 1', 'depth' => 0), array('Cat 2', 'depth' => 1), array('Cat 3', 'depth' => 1), array('Cat 4', 'depth' => 2), array('Cat 5', 'depth' => 1), array('Cat 6', 'depth' => 2), array('Cat 7', 'depth' => 3), array('Cat 8', 'depth' => 1) ); //same as before $currDepth = -1; //initilialize result $result = array(); //create path structure for depths $path = array(); //create 'root' node $olditem = array('children'=> &$result); foreach($tree as $item){ if($item['depth'] > $currDepth){ //remove possible old reference (old depth of other branch if(isset($path[$item['depth']])) unset($path[$item['depth']]); //make sure we have an array entry if(!isset($olditem['children'])) $olditem['children'] = array(); //acquire target $path[$item['depth']] = &$olditem['children']; } if($item['depth'] != $currDepth) unset($olditem); //set correct target $currDepth = $item['depth']; //add item $path[$currDepth][] = &$item; //copy & remove reference $olditem = &$item; unset($item); } //always nice to clean up reference bombs: unset($path); unset($olditem); var_dump($result); ?> с <?php $tree = array( array('Cat 1', 'depth' => 0), array('Cat 2', 'depth' => 1), array('Cat 3', 'depth' => 1), array('Cat 4', 'depth' => 2), array('Cat 5', 'depth' => 1), array('Cat 6', 'depth' => 2), array('Cat 7', 'depth' => 3), array('Cat 8', 'depth' => 1) ); //same as before $currDepth = -1; //initilialize result $result = array(); //create path structure for depths $path = array(); //create 'root' node $olditem = array('children'=> &$result); foreach($tree as $item){ if($item['depth'] > $currDepth){ //remove possible old reference (old depth of other branch if(isset($path[$item['depth']])) unset($path[$item['depth']]); //make sure we have an array entry if(!isset($olditem['children'])) $olditem['children'] = array(); //acquire target $path[$item['depth']] = &$olditem['children']; } if($item['depth'] != $currDepth) unset($olditem); //set correct target $currDepth = $item['depth']; //add item $path[$currDepth][] = &$item; //copy & remove reference $olditem = &$item; unset($item); } //always nice to clean up reference bombs: unset($path); unset($olditem); var_dump($result); ?>