php 如何从具有公共值的数组列表中构建嵌套数组?[已关闭]

xkftehaa  于 2023-01-01  发布在  PHP
关注(0)|答案(1)|浏览(155)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
Improve this question
给定一个已知的层次结构,例如:

group
  |__unit
       |__department
                |__team

我可以创建一个嵌套数组,它与给定的一组独立数组的层次结构相匹配吗?
例如,以下输入:

[
   ["group" => "group2"],
   ["group" => "group2", "unit" => "unit11", "department" => "department50", "team" => "team10"],
   ["group" => "group2", "unit" => "unit11", "department" => "department50", "team" => "team58"],
   ["group" => "group2", "unit" => "unit10"],
   ["group" => "group5", "unit" => "unit23"],
   ["group" => "group5", "unit" => "unit23", "department" => "department101"]
]

我可以迭代这些项并创建一个具有层次结构的嵌套数组吗,如下所示:

[
    "group2" => [
        "unit11" => [
            "department50" => [
                ["team10"],
                ["team58"]                
             ],
        ],    
        "unit10" => [],
    ],

    "group5" => [
        "unit23" => [
            "department101" => []                
        ],
    ],    
]

或者类似?

xqkwcwgp

xqkwcwgp1#

遍历数据并创建相应的实体。将对这些实体的引用存储在帮助器普通列表中,以便按名称访问它们。

<?php
$data = [
   ["group" => "group2"],
   ["group" => "group2", "unit" => "unit11", "department" => "department50", "team" => "team10"],
   ["group" => "group2", "unit" => "unit11", "department" => "department50", "team" => "team58"],
   ["group" => "group2", "unit" => "unit10"],
   ["group" => "group5", "unit" => "unit23"],
   ["group" => "group5", "unit" => "unit23", "department" => "department101"]
];

// Just to know how entities are to be nested
$entities = [ 'group', 'unit', 'department', 'team' ];

// Plain entities lists for convinient access
$refs = ['group' => [], 'unit' => [], 'department' => [], 'team' => []];

$root = [];

foreach( $data as $row ){
    foreach( $entities as $i => $entity ){
        if( !empty( $row[$entity] ) ){

            // If does not exist already
            if( empty( $refs[ $entity ][ $row[$entity] ] ) ){

                // Leaf entry
                if( $i == count( $entities ) - 1 ){

                    $refs[ $entities[ $i - 1 ] ][ $row[ $entities[ $i - 1 ] ] ][] = $row[$entity];

                } else{

                    unset($container);
                    $container = [];
                    // Saving ref for convinient access
                    $refs[ $entity ][ $row[$entity] ] = &$container;

                    // Intermediate Entry
                    if( $i ){                   
                        // Getting parent container
                        $refs[ $entities[ $i - 1 ] ][ $row[ $entities[ $i - 1 ] ] ][ $row[$entity] ] = &$container;
                    }
                    // Root entity
                    else{
                        $root[ $row[$entity] ] = &$container;
                    }
                }

            }
        }
    }
}

// Cleanup
unset($container);

// Result
print_r($root);

相关问题