您现在的位置是:首页 > 其他

李清波 2017-03-17 其他 2717 复制当前网址

list_to_tree将树型多维数组还原成列表

我们前面说过如何把返回的数据集转换成树型多维数组,下面我们来讲解一下如何将树型多维数组还原成列表

/**
 * 将list_to_tree的树还原成列表
 * @param  array $tree  原来的树
 * @param  string $child 孩子节点的键
 * @param  string $order 排序显示的键,一般是主键 升序排列
 * @param  array  $list  过渡用的中间数组,
 * @return array        返回排过序的列表数组
 * @author yangweijie <yangweijiester@gmail.com>
 */
function tree_to_list($tree, $child = '_child', $order='id', &$list = array()){
    if(is_array($tree)) {
        $refer = array();
        foreach ($tree as $key => $value) {
            $reffer = $value;
            if(isset($reffer[$child])){
                unset($reffer[$child]);
                tree_to_list($value[$child], $child, $order, $list);
            }
            $list[] = $reffer;
        }
        $list = list_sort_by($list, $order, $sortby='asc');
    }
    return $list;
}


文章来源:http://liqingbo.com/blog-1308.html

评论