php无限嵌套的问题

发布于 2022-09-06 04:00:55 字数 1301 浏览 15 评论 0

请问如何把$a转换成$b

$a = [
    [
        'id' => 1,
        'pid' => 0,
    ],
    [
        'id' => 2,
        'pid' => 0,
    ], [
        'id' => 3,
        'pid' => 1,
    ], [
        'id' => 4,
        'pid' => 3,
    ], [
        'id' => 5,
        'pid' => 4,
    ],
];

$b = [
    [
        'id' => 1,
        'pid' => 0,
    ],
    [
        'id' => 2,
        'pid' => 0,
    ], [
        'id' => 3,
        'pid' => 1,
        'parents' => [
            [
                'id' => 1,
                'pid' => 0,
            ],
        ],
    ], [
        'id' => 4,
        'pid' => 3,
        'parents' => [
            [
                'id' => 3,
                'pid' => 1,
            ],
            [
                'id' => 1,
                'pid' => 0,
            ],
        ],
    ], [
        'id' => 5,
        'pid' => 4,
        'parents' => [
            [
                'id' => 4,
                'pid' => 3,
            ],
            [
                'id' => 3,
                'pid' => 1,
            ],
            [
                'id' => 1,
                'pid' => 0,
            ],
        ],
    ],
];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自控 2022-09-13 04:00:55
function getParents($a,$pid){
    $parents=[];
    foreach($a as $v){
        if($v['id']==$pid){
            $parents[]=$v;
            if($v['pid']!=0){
                foreach(getParents($a,$v['pid']) as $vv){
                    $parents[]=$vv;
                }
            }
        }
    }
    return $parents;
}
function getDetail($a){
    $b=[];
    foreach ($a as $k => $v) {
        $b[]=$v;
        if($v['pid']!=0){
            $b[$k]['parents']=getParents($a,$v['pid']);
        }
    }
    return $b;
}
$b=getDetail($a);
回心转意 2022-09-13 04:00:55
$a = [
    [
        'id' => 1,
        'pid' => 0,
    ],
    [
        'id' => 2,
        'pid' => 0,
    ], [
        'id' => 3,
        'pid' => 1,
    ], [
        'id' => 4,
        'pid' => 3,
    ], [
        'id' => 5,
        'pid' => 4,
    ],
];

$b = array();
foreach ( $a as $key => $value) {
    $b[$key] = treeList($a, $value);

}


/**
 * 寻找item的家族树
 * @param $arr 数据源
 * @param $item  要寻找的元素
 * @return mixed
 */
function treeList($arr, $item){
    //如果pid为0是顶级的直接返回
    if($item['pid'] == 0){
        return $item;
    }

    //根据pid寻找父级元素
    $parent = array();
    foreach ($arr  as $value) {
        if ($item['pid'] == $value['id']) {
            $parent = $value;
        }
    }
    $item['parents'] = treeList($arr, $parent);
    return $item;
}
七七 2022-09-13 04:00:55

树形结构,网上有很多参考案例
参考链接

逐鹿 2022-09-13 04:00:55
function getParent($a,$id){
    $c = array();
    $pid = $a[$id-1]['pid'];
    while ($pid!=0){
        $c[] = $a[$pid-1];
        $pid = $a[$pid-1]['pid'];
        
    }
    return $c;
}
$b = array();
foreach ($a as $k=>$value) {
    $b[$k] = $value;
    if($value['pid']!=0){
        $b[$k]['parents'] =  getParent($a,$value['id']);
    }
}
暗喜 2022-09-13 04:00:55
    //组合多重数组
    function catePartent($cate, $name = 'parents', $pid = 4)
    {
        
        $arr = array();
        foreach($cate as $v){
            if($pid == $v["id"]){
                $v[$name]=catePartent($cate,$name,$v["pid"]);
                $arr=$v;
            }
        }
    
        return $arr;
    }
    
    //解多重数组
    function getChilds($cate,$name = 'parents') {
        //print_r($cate);
        $arr = [];
        $p=$cate;
        unset($p[$name]);
        
        if (!empty($cate[$name])) {
            $arr[]= $p;
            $arr= array_merge($arr,getChilds($cate[$name],$name));
        }
        
        if(empty($arr)){
            $arr[] = $p;
        }
        
        return $arr;
    }
    
    
    function get_p($cate)
    {
        $arr=[];
        foreach($cate as $v){
            $tmp=catePartent($cate,$name='parents',$v["pid"]);
            if( empty($tmp)){
                $arr[] = $v;
            }else{
                $v['parents']=$tmp;
                $arr[] = $v;
            }
        }

        $res=[];
        foreach($arr as $v){
            if(isset($v["parents"])){
                $v["parents"] = getChilds($v["parents"]);
            }
            $res[]=$v;
        }
        
        return $res;
    }

$a = [
    ['id' => 1,'pid' => 0,],
    ['id' => 2,'pid' => 0,], 
    ['id' => 3,'pid' => 1,], 
    ['id' => 4,'pid' => 3,], 
    ['id' => 5,'pid' => 4,],
];
var_dump(get_p($a));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文