您好,登錄后才能下訂單哦!
這篇文章給大家介紹php 中怎么實現樹形數組,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
$items = array( 0 => array('id' => 1, 'pid' => 0, 'name' => '江西省'), 1 => array('id' => 2, 'pid' => 0, 'name' => '黑龍江省'), 2 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'), 3 => array('id' => 4, 'pid' => 2, 'name' => '哈爾濱市'), 4 => array('id' => 5, 'pid' => 2, 'name' => '雞西市'), 5 => array('id' => 6, 'pid' => 4, 'name' => '香坊區'), 6 => array('id' => 7, 'pid' => 4, 'name' => '南崗區'), 7 => array('id' => 8, 'pid' => 6, 'name' => '和興路'), 8 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'), 10 => array('id' => 10, 'pid' => 8, 'name' => '東北林業大學'), 11 => array('id' => 11, 'pid' => 9, 'name' => '哈爾濱工業大學'), 12 => array('id' => 12, 'pid' => 8, 'name' => '哈爾濱師范大學'), 13 => array('id' => 13, 'pid' => 1, 'name' => '贛州市'), 14 => array('id' => 14, 'pid' => 13, 'name' => '贛縣'), 15 => array('id' => 15, 'pid' => 13, 'name' => '于都縣'), 16 => array('id' => 16, 'pid' => 14, 'name' => '茅店鎮'), 17 => array('id' => 17, 'pid' => 14, 'name' => '大田鄉'), 18 => array('id' => 18, 'pid' => 16, 'name' => '義源村'), 19 => array('id' => 19, 'pid' => 16, 'name' => '上壩村'), ); function getTree($items) { $tree = []; foreach ($items as $item) { $tree[$item['id']] = $item; } foreach ($tree as $key => $val) { $tree[$val['pid']]['child'][$key] = &$tree[$key]; } return $tree['0']['child']; } print_r(getTree($items));
關于php 中怎么實現樹形數組就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。