0

我有一个这样的数组

Array
(
 [0] => Array
    (
        [employee_id] => 2
        [project_name] => WeeFavR
        [project_planned_hour] => 500
        [module_name] => Design
        [project_module_time] => 150
        [allocated_time] => 50
    )

[1] => Array
    (
        [employee_id] => 2
        [project_name] => WeeFavR
        [project_planned_hour] => 500
        [module_name] => Coding
        [project_module_time] => 250
        [allocated_time] => 200
    )

[2] => Array
    (
        [employee_id] => 2
        [project_name] => WeeFavR
        [project_planned_hour] => 500
        [module_name] => Testing
        [project_module_time] => 100
        [allocated_time] => 50
    )

[3] => Array
    (
        [employee_id] => 2
        [project_name] => Online License
        [project_planned_hour] => 600
        [module_name] => Design
        [project_module_time] => 200
        [allocated_time] => 100
    )

[4] => Array
    (
        [employee_id] => 2
        [project_name] => Online License
        [project_planned_hour] => 600
        [module_name] => Coding
        [project_module_time] => 200
        [allocated_time] => 100
    )

[5] => Array
    (
        [employee_id] => 2
        [project_name] => Online License
        [project_planned_hour] => 600
        [module_name] => Testing
        [project_module_time] => 200
        [allocated_time] => 100
    )
)

我想要这样的结果

Array
(
[0] => Array
    (
        [employee_id] => 2
        [project_name] => WeeFavR
        [project_planned_hour] => 500
        Array
        (
            [0] => Array
                (
                    [module_name] => Design
                    [project_module_time] => 150
                    [allocated_time] => 50
                )
             [1] => Array
                (
                    [module_name] => Coding
                    [project_module_time] => 250
                    [allocated_time] => 200
                )
             [2] => Array
                (
                    [module_name] => Testing
                    [project_module_time] => 100
                    [allocated_time] => 50
                )       
        )
    )

[1] => Array
    (
        [employee_id] => 2
        [project_name] => Online License
        [project_planned_hour] => 600
        Array
        (
            [0] => Array
                (
                    [module_name] => Design
                    [project_module_time] => 200
                    [allocated_time] => 100
                )
             [1] => Array
                (
                    [module_name] => Coding
                    [project_module_time] => 200
                    [allocated_time] => 100
                )
             [2] => Array
                (
                    [module_name] => Testing
                    [project_module_time] => 200
                    [allocated_time] => 100
                )
        )
    )
)

我不知道如何制作这种类型的数组,所以请帮我解决这个问题。您的帮助将不胜感激。

4

5 回答 5

1

看起来您想要做的实际上是遍历您的数组并根据项目对其进行分组。

如果您设置了一个关联数组来查找您的项目,这很容易。

$grouped_data   = array();
foreach ($data as $item) {
    //we'll used the project name as the key
    $key        = $item['project_name'];
    if (empty($grouped_data[$key])) {
        //this project isn't created yet, create it
        $grouped_data[$key]         = $item;
        $grouped_data[$key]['modules']  = array(); //create the module sub array to store things
    }
    $grouped_data[$key]['modules'][]        = $item; //store your module for this project 
}

当然,您需要自己做一些工作来清理您存储在子数组中的内容。目前它存储所有内容,因此一些键是重复的。但这应该让您毫无问题地开始。

于 2013-08-06T11:49:47.527 回答
0

就像您可以将变量保存在数组中一样,您也可以将数组保存在数组中:

$someArray = array(
    'Value 1',
    array(
        array(
            'Some more value,
            'more'
        ),
        array(
            'Some more value,
            'more'
        )
    )
);
于 2013-08-06T11:41:12.097 回答
0

你可以做的是这样的:

    $arr_project = array ();
    $arr_example_tmp =  array ("module_name" => "Design",
                        "project_module_time" => 200,
                        "allocated_time" => 100);


    $arr_project["project_planned_hour"][] = $arr_example_tmp;


    $arr_example_tmp =  array ("module_name" => "Coding",
                        "project_module_time" => 200,
                        "allocated_time" => 100);

   $arr_project["project_planned_hour"][] = $arr_example_tmp;
于 2013-08-06T11:42:37.790 回答
0

确保你的模块有一些索引

$myArray = Array();    

$myArray[0] = Array(    
    "employee_id" => 2,    
    "project_name" => "WeeFavR",    
    "project_planned_hour" => 500,    
    "modules" => Array(    
        Array(    
            "module_name" => "Design",    
            "project_module_time" => 150,    
            "allocated_time" => 50    
        ),    
    ),    
);    
于 2013-08-06T11:45:45.963 回答
0
$out = array();
foreach ($arr as $key => $value){
    $dex = $value['employee_id'].' - '.$value['project_name'];
    if ( ! isset($out[$dex])){
        $out[$dex] = array_slice($value, 0, 3);
    }
    $out[$dex]['details'][] = array_slice($value, 3);
}
$out = array_values($out);
print_r($out);
于 2013-08-06T12:45:04.240 回答