The code:

<? PHP /** * Format data into a tree * @author xuefen.tong * @param array$items
 * @return array
 */
function genTree5($items) {
    foreach ($items as $item) {$items[$item['pid']] ['son'] [$item['id']] = &$items[$item['id']];
    }
    return isset($items[0] ['son'])?$items[0] ['son'] : array();
}

function genTree9($items) {
    $tree= array(); // foreach ()$items as $item)
        if (isset($items[$item['pid']]))
            $items[$item['pid']] ['son'] [] = &$items[$item['id']].else
            $tree[] = &$items[$item['id']].return $tree; } // Test code:$items = array(
=> array('id'= > 6,'pid'= > 4,'name'= >'Xiangfang District'),
=> array('id'= > 1,'pid'= > 0,'name'= >'Jiangxi'),
=> array('id'= > 2,'pid'= > 0,'name'= >'Heilongjiang province'),
=> array('id'= > 3,'pid'= > 1,'name'= >'Nanchang'),
=> array('id'= > 4,'pid'= > 2,'name'= >'Harbin'),
//            5 => array('id'= > 5,'pid'= > 2,'name'= >Jixi City),
//
=> array('id'= > 7,'pid'= > 4,'name'= >'Nangang District'),
//            8 => array('id'= > 8,'pid'= > 6,'name'= >Wo Hing Road),
//            9 => array('id'= > 9,'pid'= > 7,'name'= >'West Tai Chi Street'),
//            10 => array('id'= > 10,'pid'= > 8,'name'= >'Northeast Forestry University'),
//            11 => array('id'= > 11,'pid'= > 9,'name'= >'Harbin Institute of Technology'),
//            12 => array('id'= > 12,'pid'= > 8,'name'= >'Harbin Normal University'),
//            13 => array('id'= > 13,'pid'= > 1,'name'= >'Ganzhou'),
//            14 => array('id'= > 14,'pid'= > 13,'name'= >'county'),
//            15 => array('id'= > 15,'pid'= > 13,'name'= >Yudu County),
//            16 => array('id'= > 16,'pid'= > 14,'name'= >'Maodian Town'),
//            17 => array('id'= > 17,'pid'= > 14,'name'= >'Datian Village'),
//            18 => array('id'= > 18,'pid'= > 16,'name'= >'Yiyuan Village'),
//            19 => array('id'= > 19,'pid'= > 16,'name'= >'Shangba Village'));echo "<pre>";

        print_r(genTree5($items));Copy the code


The above contents hope to help you