如果父ID相同,则合并多维数组,然后合并子数据

时间:2016-06-17 13:37:38

标签: php arrays

我想转换下面的数组。

标准是,

如果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
                    )
                )
            )
        )
)

2 个答案:

答案 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)

见下面的例子

Example

$data = [];

foreach ($a as $b) {
    $key = $b["PMSP"]["id"];
    if (!isset($data[$key])) {
        $data[$key] = ["PMSP" => $b["PMSP"], "taskdetails" => []];
    }
    $data[$key]["taskdetails"][] = $b["PMST"];
}
echo "<pre>";
print_r($data);exit();

您的最新数据 Example 2