我想转换下面的数组。
标准是,
如果projectId
相同,则将包含projectId
的项目数组下的相同项目ID数据存储为关键字。
Array
(
[0] => Array
(
[PMST] => Array
(
[id] => 4
[project_id] => 25
[task_name] => Final task 3
[start_date] => 2016-06-21 00:00:00
[end_date] => 2016-06-29 00:00:00
)
[PMSP] => Array
(
[id] => 25
[project_name] => Project 3
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
)
[1] => Array
(
[PMST] => Array
(
[id] => 9
[project_id] => 28
[task_name] => Task Test 333 edit
[start_date] => 2016-06-19 00:00:00
[end_date] => 2016-06-29 00:00:00
)
[PMSP] => Array
(
[id] => 28
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
)
[2] => Array
(
[PMST] => Array
(
[id] => 1
[project_id] => 28
[task_name] => Task 1
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
[PMSP] => Array
(
[id] => 28
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
)
)
欲望输出
Array
(
[25] => Array
(
[PMSP] => Array
(
[id] => 25
[company_id] => 1114701
[project_name] => Project 3
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
[taskdetails] => Array
(
[0] => Array(
[PMST] => Array
(
[id] => 4
[project_id] => 25
[company_id] => 1114701
[task_name] => Final task 3
[start_date] => 2016-06-21 00:00:00
[end_date] => 2016-06-29 00:00:00
)
)
)
)
[28] => Array
(
[PMSP] => Array
(
[id] => 28
[company_id] => 1114701
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
[taskdetails] => Array
(
[0] => Array
(
[PMST] => Array
(
[id] => 9
[project_id] => 28
[company_id] => 1114701
[task_name] => Task Test 333 edit
[start_date] => 2016-06-19 00:00:00
[end_date] => 2016-06-29 00:00:00
)
)
[1] => Array(
[PMST] => Array
(
[id] => 1
[project_id] => 28
[company_id] => 1114701
[task_name] => Task 1
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
)
)
)
)
答案 0 :(得分:2)
使用foreach
遍历输入数组,随时将PMST
数据添加到输出数组。假设同一项目中两个任务的PMSP
数据是相同的,这似乎是安全的。
$output = [];
foreach ($input as $task) {
if (!isset($output[$task["PMSP"]["id"]])) {
$output[$task["PMSP"]["id"]] = ["PMSP" => $task["PMSP"], "taskdetails" => []];
}
$output[$task["PMSP"]["id"]]["taskdetails"][] = $task["PMST"];
}
答案 1 :(得分:2)