Php 根据父级重新排列数组

Php 根据父级重新排列数组,php,Php,我有一个包含此数据的示例数组 如何重新排列此数组,使所有子数组自动跟随其父数组,如下所示 我起草了一个解决方案(可能不是最优的,但它适合您的情况): 我起草了一个解决方案(可能不是最优的,但它适合您的情况): 另一个快速解决方案是构建父子映射,这样我们可以更快、更容易地生成层次结构 $people=array( ['id'=>153,'caption'=>'Parent A','Parent_id'=>0], ['id'=>155,'caption'=>'Parent B','Parent_i

我有一个包含此数据的示例数组

如何重新排列此数组,使所有子数组自动跟随其父数组,如下所示

我起草了一个解决方案(可能不是最优的,但它适合您的情况):

我起草了一个解决方案(可能不是最优的,但它适合您的情况):


另一个快速解决方案是构建父子映射,这样我们可以更快、更容易地生成层次结构

$people=array(
['id'=>153,'caption'=>'Parent A','Parent_id'=>0],
['id'=>155,'caption'=>'Parent B','Parent_id'=>0],
['id'=>159,'caption'=>'Parent A Child 1','Parent_id'=>153],
['id'=>160,'caption'=>'Parent A Child 2','Parent_id'=>153],
['id'=>161,'caption'=>'Parent A Child 3','Parent_id'=>153],
['id'=>162,'caption'=>'Parent B Child 1','Parent_id'=>155],
['id'=>163,'caption'=>'Parent B Child 2','Parent_id'=>155],
['id'=>166,'caption'=>'Parent A Child 1孙子1','Parent_id'=>159],
['id'=>167,'caption'=>'Parent A Child 1孙子2','Parent_id'=>159],
);
$formatted=[];
$childs=[];
foreach($people as$data){
//数组将每个人的数据作为值保存,将其id作为键保存,所以我们只需使用数组中的键即可获取数据。
$formatted[$data['id']]=$data;
//一种关联数组,其中父项id作为键,子项作为该键处的数组。
$childs[$data['parent_id'][]=$data['id'];
}
//考虑到所有第一级人员的家长id为0。从根节点(0)开始父子遍历。
$final=getChilds(0);
//使用深度优先搜索使用递归获取child。
函数getChilds($parentId){
全局$childs,$formatted;
if(isset($childs[$parentId])){
$parentChild=[];
foreach($childs[$parentId]作为$child){
$parentChild[]=$formatted[$child];
$parentChild=array_merge($parentChild,getChilds($child));
}
返回$parentChild;
}
否则{
返回[];
}
}
回声';打印(最终版);死亡
输出:

数组
(
[0]=>阵列
(
[id]=>153
[标题]=>父级A
[parent_id]=>0
)
[1] =>阵列
(
[id]=>159
[标题]=>父对象是子对象1
[家长id]=>153
)
[2] =>阵列
(
[id]=>166
[说明]=>为孩子的父母1孙子1
[家长id]=>159
)
[3] =>阵列
(
[id]=>167
[说明]=>父母一个孩子一个孙子2个
[家长id]=>159
)
[4] =>阵列
(
[id]=>160
[标题]=>父级A子级2
[家长id]=>153
)
[5] =>阵列
(
[id]=>161
[说明]=>父母与子女3
[家长id]=>153
)
[6] =>阵列
(
[id]=>155
[标题]=>父级B
[parent_id]=>0
)
[7] =>阵列
(
[id]=>162
[标题]=>父B子1
[家长id]=>155
)
[8] =>阵列
(
[id]=>163
[标题]=>父B子2
[家长id]=>155
)
)

另一个快速解决方案是构建父子映射,这样我们可以更快、更轻松地生成层次结构

$people=array(
['id'=>153,'caption'=>'Parent A','Parent_id'=>0],
['id'=>155,'caption'=>'Parent B','Parent_id'=>0],
['id'=>159,'caption'=>'Parent A Child 1','Parent_id'=>153],
['id'=>160,'caption'=>'Parent A Child 2','Parent_id'=>153],
['id'=>161,'caption'=>'Parent A Child 3','Parent_id'=>153],
['id'=>162,'caption'=>'Parent B Child 1','Parent_id'=>155],
['id'=>163,'caption'=>'Parent B Child 2','Parent_id'=>155],
['id'=>166,'caption'=>'Parent A Child 1孙子1','Parent_id'=>159],
['id'=>167,'caption'=>'Parent A Child 1孙子2','Parent_id'=>159],
);
$formatted=[];
$childs=[];
foreach($people as$data){
//数组将每个人的数据作为值保存,将其id作为键保存,所以我们只需使用数组中的键即可获取数据。
$formatted[$data['id']]=$data;
//一种关联数组,其中父项id作为键,子项作为该键处的数组。
$childs[$data['parent_id'][]=$data['id'];
}
//考虑到所有第一级人员的家长id为0。从根节点(0)开始父子遍历。
$final=getChilds(0);
//使用深度优先搜索使用递归获取child。
函数getChilds($parentId){
全局$childs,$formatted;
if(isset($childs[$parentId])){
$parentChild=[];
foreach($childs[$parentId]作为$child){
$parentChild[]=$formatted[$child];
$parentChild=array_merge($parentChild,getChilds($child));
}
返回$parentChild;
}
否则{
返回[];
}
}
回声';打印(最终版);死亡
输出:

数组
(
[0]=>阵列
(
[id]=>153
[标题]=>父级A
[parent_id]=>0
)
[1] =>阵列
(
[id]=>159
[标题]=>父对象是子对象1
[家长id]=>153
)
[2] =>阵列
(
[id]=>166
[说明]=>为孩子的父母1孙子1
[家长id]=>159
)
[3] =>阵列
(
[id]=>167
[说明]=>父母一个孩子一个孙子2个
[家长id]=>159
)
[4] =>阵列
(
[id]=>160
[标题]=>父级A子级2
[家长id]=>153
)
[5] =>阵列
(
[id]=>161
[说明]=>父母与子女3
<?php
$people = array(
    [ 'id' => 153, 'caption' => 'Parent A', 'parent_id' => 0],
    [ 'id' => 155, 'caption' => 'Parent B', 'parent_id' => 0],
    [ 'id' => 159, 'caption' => 'Parent A Child 1', 'parent_id' => 153],
    [ 'id' => 160, 'caption' => 'Parent A Child 2', 'parent_id' => 153],
    [ 'id' => 161, 'caption' => 'Parent A Child 3', 'parent_id' => 153],
    [ 'id' => 162, 'caption' => 'Parent B Child 1', 'parent_id' => 155],
    [ 'id' => 163, 'caption' => 'Parent B Child 2', 'parent_id' => 155],
    [ 'id' => 166, 'caption' => 'Parent A Child 1 Grandchild 1', 'parent_id' => 159],
    [ 'id' => 167, 'caption' => 'Parent A Child 1 Grandchild 2', 'parent_id' => 159],
);



function cmp($a, $b) {
    return strcmp($a['parent_id'], $b['parent_id']);
}

// Step 1: Sort people by "PARENT ID" (optional, as it's already sorted)
usort($people, "cmp");

// Step 2: Find all childs by parent ID
$temp = array();
foreach($people as $key => $person) {
    if($person['parent_id'] == 0) {
        $temp[] = $person; // add the "Parent A" first
        unset($people[$key]); // remove the added parent
        $children = getPersonByParentId($people, $person['id']);
        foreach($children as $key2 => $child) {
            $temp[] = $child;
            unset($people[$key2]);
            $grandchildren = getPersonByParentId($people, $child['id']);
            foreach($grandchildren as $key3 => $grandchild) {
                $temp[] = $grandchild;
                unset($people[$key3]);
            }
        }
    }
}

function getPersonByParentId(&$people, $id) {
    $result = array();
    foreach($people as $key => $person) {
        if($person['parent_id'] == $id) {
            $result[] = $person;
            unset($people[$key]); // remove the selected one
        }
    }
    return $result;
}

print_r($temp);

?>
Array
(
    [0] => Array
        (
            [id] => 153
            [caption] => Parent A
            [parent_id] => 0
        )

    [1] => Array
        (
            [id] => 159
            [caption] => Parent A Child 1
            [parent_id] => 153
        )

    [2] => Array
        (
            [id] => 166
            [caption] => Parent A Child 1 Grandchild 1
            [parent_id] => 159
        )

    [3] => Array
        (
            [id] => 167
            [caption] => Parent A Child 1 Grandchild 2
            [parent_id] => 159
        )

    [4] => Array
        (
            [id] => 160
            [caption] => Parent A Child 2
            [parent_id] => 153
        )

    [5] => Array
        (
            [id] => 161
            [caption] => Parent A Child 3
            [parent_id] => 153
        )

    [6] => Array
        (
            [id] => 155
            [caption] => Parent B
            [parent_id] => 0
        )

    [7] => Array
        (
            [id] => 162
            [caption] => Parent B Child 1
            [parent_id] => 155
        )

    [8] => Array
        (
            [id] => 163
            [caption] => Parent B Child 2
            [parent_id] => 155
        )

)
<pre>Array
(
    [0] => Array
        (
            [id] => 153
            [caption] => Parent A
            [parent_id] => 0
        )

    [1] => Array
        (
            [id] => 159
            [caption] => Parent A Child 1
            [parent_id] => 153
        )

    [2] => Array
        (
            [id] => 166
            [caption] => Parent A Child 1 Grandchild 1
            [parent_id] => 159
        )

    [3] => Array
        (
            [id] => 167
            [caption] => Parent A Child 1 Grandchild 2
            [parent_id] => 159
        )

    [4] => Array
        (
            [id] => 160
            [caption] => Parent A Child 2
            [parent_id] => 153
        )

    [5] => Array
        (
            [id] => 161
            [caption] => Parent A Child 3
            [parent_id] => 153
        )

    [6] => Array
        (
            [id] => 155
            [caption] => Parent B
            [parent_id] => 0
        )

    [7] => Array
        (
            [id] => 162
            [caption] => Parent B Child 1
            [parent_id] => 155
        )

    [8] => Array
        (
            [id] => 163
            [caption] => Parent B Child 2
            [parent_id] => 155
        )

)