您好,登錄后才能下訂單哦!
這篇文章主要介紹了根據引用日均實現索引樹的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
$array = array( 0=>array('id'=>1,'uid'=>0,'menuname'=>'菜單1','url'=>0,'addtime'=>'2018-08-29 16:36:55'), 1=>array('id'=>2,'uid'=>0,'menuname'=>'菜單2','url'=>0,'addtime'=>'2018-08-29 16:36:55'), 2=>array('id'=>3,'uid'=>1,'menuname'=>'1子菜單1','url'=>0,'addtime'=>'2018-08-29 16:36:55'), 3=>array('id'=>4,'uid'=>3,'menuname'=>'1子菜單2','url'=>0,'addtime'=>'2018-08-29 16:36:55'), 4=>array('id'=>5,'uid'=>2,'menuname'=>'2子菜單1','url'=>0,'addtime'=>'2018-08-29 16:36:55'), ); function getTree($items,$pid ="uid") { $map = []; $tree = []; foreach ($items as &$it) $map[$it['id']] = &$it; //數據的ID名生成新的引用索引樹 foreach ($items as $k => &$at){ $parent = &$map[$at[$pid]]; if($parent) { $parent['children'][] = &$at; }else{ $tree[] = &$at; } } return $tree; } print_r(getTree($array));die;
出來的效果是
Array ( [0] => Array ( [id] => 1 [uid] => 0 [menuname] => 菜單1 [url] => 0 [addtime] => 2018-08-29 16:36:55 [children] => Array ( [0] => Array ( [id] => 3 [uid] => 1 [menuname] => 1子菜單1 [url] => 0 [addtime] => 2018-08-29 16:36:55 [children] => Array ( [0] => Array ( [id] => 4 [uid] => 3 [menuname] => 1子菜單2 [url] => 0 [addtime] => 2018-08-29 16:36:55 ) ) ) ) ) [1] => Array ( [id] => 2 [uid] => 0 [menuname] => 菜單2 [url] => 0 [addtime] => 2018-08-29 16:36:55 [children] => Array ( [0] => Array ( [id] => 5 [uid] => 2 [menuname] => 2子菜單1 [url] => 0 [addtime] => 2018-08-29 16:36:55 ) ) ) )
感謝你能夠認真閱讀完這篇文章,希望小編分享的“根據引用日均實現索引樹的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。