php从单个数组创建多维数组

时间:2015-01-15 10:42:27

标签: php arrays multidimensional-array

我有单个阵列中的工作人员数组列表,我正在尝试基于直线管理器制作多维度。因此,在我的例子中,Miriam Wood负责管理Alan Haworth和Guy Kahane,Guy Kahane根据Line Manger领域管理Matthew Baum和Rebecca Roache。所以我现在的阵列看起来像这样。

希望从

做一些事情
  1. Miriam Wood - 没有直线经理
  2. Alan Hanworth - 直线经理 - > id 1
  3. Guy Kahane - 直线经理 - > id 1
  4. Matthew Baum - 直线经理 - > id 3
  5. Rebecca Roache - 直线经理 - > id 3
    • Miriam Wood
      • Alan Hanworth
      • Guy Kahane
        • Matthew Baum
        • Rebecca Roache


    [98] => Array ([Id] => 1, [Name] => Miriam Wood, [Line Manager] => None)
    
    [99] => Array([Id] => 2, [Name] => Alan Haworth, [Line Manager] => 1)
    
    [105] => Array([Id] => 3, [Name] => Guy Kahane, [Line Manager] => 1)
    
    [106] => Array([Id] => 4, [Name] => Matthew Baum, [Line Manager] => 3)
    
    [107] => Array([Id] => 5, [Name] => Rebecca Roache, [Line Manager] => 3)
    

    我想将其改为

    (
     [98] => Array
    (
       [Id] => 1
       [Name] => Miriam Wood
       [Line Manager] => None
       [Staffs] => Array
    (
        [99] => Array
        (
            [Id] => 2
            [Name] => Alan Haworth
            [Line Manager] => 1
        )
    
        [105] => Array
        (
            [Id] => 3
            [Name] => Guy Kahane
            [Line Manager] => 1
            [staffs] => Array
            (
                [106] => Array
                (
                    [Id] => 4
                    [Name] => Matthew Baum
                    [Line Manager] => 3
                )
    
                [107] => Array
                (
                    [Id] => 5
                    [Name] => Rebecca Roache
                    [Line Manager] => 3
                )
            )
        )
    )
    

    ) )

2 个答案:

答案 0 :(得分:0)

这是一个简单的例子。

$head = array("id">1,"name"=>"Harshana","servents"=>array(
"s1"=>array("id">1,"name"=>"abc"),
"s2"=>array("id">2,"name"=>"sdf"),
"s3"=>array("id">3,"name"=>"fgh")
));
print_r($head);
echo "<hr />";
print_r($head["servents"]);
echo "<hr />";
print_r($head["servents"]["s1"]);
echo "<hr />";
$s1 = $head["servents"]["s1"];
echo "servent 1 name :".$s1["name"];

希望你能对自己的问题有所了解。

答案 1 :(得分:0)

我试图解决你的问题。我已经完成了3个功能,可以将每个功能的职责降到最低。它看起来如下:

function build_employee_hierarchy(array $employees) {

    $groups    = build_employee_groups($employees);
    $hierarchy = build_employee_group_inheritance($employees, $groups);

    return $hierarchy[0];

}

function build_employee_group_inheritance(array $employees, array $groups) {

    $hierarchy = [];

    foreach($employees as $key => $employee) {

        $employee_id     = $employee['id'];
        $hierarchy[$key] = $employee;

        if(!array_key_exists($employee_id, $groups)) {
            continue;
        }

        $hierarchy[$key]['staff'] = build_employee_group_inheritance($groups[$employee_id], $groups);

    }

    return $hierarchy;
}

function build_employee_groups(array $employees) {

    $groups = [];

    foreach($employees as $employee) {

        if(array_key_exists('line-manager', $employee) && !is_null($employee['line-manager'])) {

            $groups[$employee['line-manager']][] = $employee;

        }

    }

    return $groups;

}

这些功能首先将属于同一直线经理的所有员工分组。然后递归地解决员工的继承问题。函数build_employee_hierarchy用作通过提供单个数组来构建整个层次结构的实用方法。

根据您提供的数据,结果将如下所示。 唯一区别是,当没有直线经理时,它等于NULL而不是字符串none

array (size=4)
  'id' => int 1
  'name' => string 'Miriam Wood' (length=11)
  'line-manager' => null
  'staff' => 
    array (size=2)
      0 => 
        array (size=3)
          'id' => int 2
          'name' => string 'Alan Haworth' (length=12)
          'line-manager' => int 1
      1 => 
        array (size=4)
          'id' => int 3
          'name' => string 'Guy Kahane' (length=10)
          'line-manager' => int 1
          'staff' => 
            array (size=2)
              0 => 
                array (size=3)
                  'id' => int 4
                  'name' => string 'Matthew Baum' (length=12)
                  'line-manager' => int 3
              1 => 
                array (size=3)
                  'id' => int 5
                  'name' => string 'Rebecca Roache' (length=14)
                  'line-manager' => int 3

这可能不是最有效的解决方案,但因为如果使用递归,则数组可以是您想要的长。

<强> OBS

如果数据来自关系数据库,则很可能使用数据库使用的SQL语言以更有效的方式检索同一表单中的数据。如果是这种情况,请在Database Administrators (Stackexchange)询问优秀的人。

希望这有帮助。