這篇文章主要介紹了根據(jù)引用日均實現(xiàn)索引樹的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司專注于夾江網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供夾江營銷型網(wǎng)站建設(shè),夾江網(wǎng)站制作、夾江網(wǎng)頁設(shè)計、夾江網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造夾江網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供夾江網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
$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; //數(shù)據(jù)的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 ) ) ) )
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“根據(jù)引用日均實現(xiàn)索引樹的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!