我有一个像这样的数组
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
)
)
)
)
我不知道如何制作这种类型的数组,所以请帮我解决这个问题。 感谢您的帮助。
答案 0 :(得分: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
}
当然,您需要自己做一些工作来清理您在子数组中存储的内容。目前它存储了所有内容,因此一些密钥是重复的。但是这应该让你没有问题。
答案 1 :(得分:0)
就像您可以在数组中保存变量一样,您也可以将数组保存在数组中:
$someArray = array(
'Value 1',
array(
array(
'Some more value,
'more'
),
array(
'Some more value,
'more'
)
)
);
答案 2 :(得分: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;
答案 3 :(得分: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
),
),
);
答案 4 :(得分: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);