onethink中 list_to_tree 函式 是如何返回陣列的

發表於2019-05-11
function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
    // 建立Tree
    $tree = array();
    if(is_array($list)) {
        // 建立基於主鍵的陣列引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            // 判斷是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];
            }else{
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
                    $parent[$child][] =& $list[$key];
                }
            }
        }
    }
    return $tree;
}
回覆

相關文章