如何在PHP中合并两个不相等的多维数组?

我有两个数组,如 JS 小提琴中给出的多维数组。如果 Month1 值相同,我想按升序将其合并为一个,否则按原样打印


    $array1= [0] => Array

            (

                [month1] => January

                [2020cs] => 84

                [2020as] => 500

                [2019cs] => 17

                [2019as] => 500

            )

    

        [1] => Array

            (

                [month1] => February

                [2020cs] => 54

                [2020as] => 200

                [2019cs] => 12

                [2019as] => 1000

            )

    

        [2] => Array

            (

                [month1] => April

                [2020cs] => 4

                [2020as] => 100

                [2019cs] => 12

                [2019as] => 1400

            )

    

        [3] => Array

            (

                [month1] => November

                [2020cs] => 0

                [2020as] => 0

                [2019cs] => 7

                [2019as] => 200

            )

    )


   

         $array2= [0] => Array

            (

                [month1] => January

                [2020cr] => 13

                [2020ar] => 300

                [2019cr] => 0

                [2019ar] => 0

            )

    

        [2] => Array

            (

                [month1] => March

                [2020cr] => 1

                [2020ar] => 100

                [2019cr] => 0

                [2019ar] => 0

            )

    

        [3] => Array

            (

                [month1] => November

                [2020cr] => 0

                [2020ar] => 0

                [2019cr] => 1

                [2019ar] => 800

            )

    

        [4] => Array

            (

                [month1] => December

                [2020cr] => 0

                [2020ar] => 0

                [2019cr] => 2

                [2019ar] => 500

            )

    

    )

我也使用过 array_merge_recursive() 。它也不起作用。谁能帮我吗?提前致谢。


叮当猫咪
浏览 119回答 1
1回答

慕的地10843

以下逻辑可能会帮助您:为每个顶级数组键指定月份名称 (&nbsp;rekey())合并两个数组对结果集进行排序 (&nbsp;$result)<?php// top-level array from indexed to associative (name of month)$array1 = rekey($array1);$array2 = rekey($array2);function rekey(array $arr = []): array{&nbsp; &nbsp; foreach ($arr as $key => $record) {&nbsp; &nbsp; &nbsp; &nbsp; $arr[$record['month1']] = $arr[$key]; // indexed to name of month&nbsp; &nbsp; &nbsp; &nbsp; unset($arr[$key]);&nbsp; &nbsp; }&nbsp; &nbsp; return $arr;}// run the merge$result = array_merge_recursive($array1, $array2);// clean up double entries key 'month1'foreach($result as $key => &$value) {&nbsp; &nbsp; if(is_array($value['month1'])) $value['month1'] = $value['month1'][0];}// prepare result set for sorting$months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'August', 'September', 'October', 'November', 'December'];foreach($result as $key0 => $value0) {&nbsp; &nbsp; foreach($months as $key1 => $value1) {&nbsp; &nbsp; &nbsp; &nbsp; if($key0 === $value1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$key1] = $result[$key0]; // set index based on month 0-11&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($result[$key0]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}ksort($result); // sort$result = array_values($result); // top-level array back to sequential index工作演示
打开App,查看更多内容
随时随地看视频慕课网APP