简体   繁体   中英

How can I merge between two multidimensional array respected their keys

Let's say I have following arrays:

Array
(
    [2001] => Array
        (
            [event_detail] => Array
                (
                    [1] => Array
                        (
                            [event_name] => Advocacy CPD 2019
                            [cpd_points] => 23
                        )

                    [5] => Array
                        (
                            [event_name] => Advocacy CPD 2019 new
                            [cpd_points] => 2
                        )

                )

        )

    [2002] => Array
        (
            [event_detail] => Array
                (
                    [2] => Array
                        (
                            [event_name] => Advocacy CPD 2011
                            [cpd_points] => 28
                        )

                    [4] => Array
                        (
                            [event_name] => Advocacy CPD 20195 new
                            [cpd_points] => 12
                        )

                )

        )

)

And the second one:

Array
(
    [2001] => Array
        (
            [event_detail] => Array
                (
                    [1] => Array
                        (
                            [event_name] => Advocacy CPD 2020
                            [cpd_points] => 27
                        )

                )

        )

    [2052] => Array
        (
            [event_detail] => Array
                (
                    [2] => Array
                        (
                            [event_name] => Advocacy CPD 2052
                            [cpd_points] => 258
                        )

                    [4] => Array
                        (
                            [event_name] => Advocacy CPD 2019445 new
                            [cpd_points] => 78
                        )

                )

        )

)

And desired output is:

Array
(
    [2001] => Array
        (
            [event_detail] => Array
                (
                    [1] => Array
                        (
                            [event_name] => Advocacy CPD 2019
                            [cpd_points] => 23
                        )

                    [5] => Array
                        (
                            [event_name] => Advocacy CPD 2019 new
                            [cpd_points] => 2
                        )

                    [6] => Array
                        (
                            [event_name] => Advocacy CPD 2020
                            [cpd_points] => 27
                        )

                )

        )

    [2002] => Array
        (
            [event_detail] => Array
                (
                    [2] => Array
                        (
                            [event_name] => Advocacy CPD 2011
                            [cpd_points] => 28
                        )

                    [4] => Array
                        (
                            [event_name] => Advocacy CPD 20195 new
                            [cpd_points] => 12
                        )

                )

        )

    [2052] => Array
        (
            [event_detail] => Array
                (
                    [2] => Array
                        (
                            [event_name] => Advocacy CPD 2052
                            [cpd_points] => 258
                        )

                    [4] => Array
                        (
                            [event_name] => Advocacy CPD 2019445 new
                            [cpd_points] => 78
                        )

                )

        )

)

The problem is I would like to merge these arrays on the same id[2001,2005,2002]. Desired output sorting should be the same as in the first array.

I have managed to merge these arrays with different way but failed:

How can I achieve this? Any help is much appreciated.

Pretty simple, just loop the second array and merge using the key to the first array:

foreach($array2 as $key => $val) {
    $array1[$key]['event_detail'] = array_merge($array1[$key]['event_detail'],
                                                $val['event_detail']);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM