如何在PHP中合并数组?

jvlzgdj9  于 2023-03-28  发布在  PHP
关注(0)|答案(2)|浏览(126)

我有两个JSON格式,我将JSON转换为数组,然后合并它,但它不工作。
见下面两个json格式。
下面是我的PHP代码,它没有合并数组,这里有什么错?

$arr1 = json_decode('{
   "PatchDeployment":[
      {
         "ehubid":"123",
         "tomcats":[
           
            {
               "tomcatname":"mytomcat",
               "servername":"myserver",
               "serverip":"xx.xx.xx.xxx",
               "tomcat_statuses":{
                  "stoptomcat":"Success"
               }
            }
         ]
      }
   ]
}', true);

$arr2 = json_decode('{
   "PatchDeployment":[
      {
         "ehubid":"123",
         "tomcats":[
            {
               "tomcatname":"mytomcat",
               "servername":"myserver",
               "serverip":"xx.xx.xx.xxx",
               "tomcat_statuses":{
                  "checkdb":"Failed",
                  "checkhub":"couldnt connect to host"
               }
            }
            
         ]
      }
   ]
}', true);

$mergedarray = array_merge($arr1, $arr2);

我希望合并$mergedarray后的数组如下所示:

Array
(
    [PatchDeployment] => Array
        (
            [0] => Array
                (
                    [ehubid] => 123
                    [tomcats] => Array
                        (
                            [0] => Array
                                (
                                    [tomcatname] => mytomcat
                                    [servername] => myserver
                                    [serverip] => xx.xx.xx.xxx
                                    [tomcat_statuses] => Array
                                        (
                                            [checkdb] => Failed
                                            [checkhub] => couldnt connect to host
                                            [stoptomcat]=> Success
                                        )

                                )

                        )

                )

        )

)

我正在把两个数组合并到php中
谁能帮我尽快解决这个问题?

fhg3lkii

fhg3lkii1#

您不希望它们重复。您正在寻找的方法:array_replace_recursive

代码

$arr1 = json_decode('{
    "PatchDeployment":[
        {
            "ehubid":"123",
            "tomcats":[

                {
                "tomcatname":"mytomcat",
                "servername":"myserver",
                "serverip":"xx.xx.xx.xxx",
                "tomcat_statuses":{
                    "stoptomcat":"Success"
                }
                }
            ]
        }
    ]
}', true);

$arr2 = json_decode('{
    "PatchDeployment":[
        {
            "ehubid":"123",
            "tomcats":[
                {
                "tomcatname":"mytomcat",
                "servername":"myserver",
                "serverip":"xx.xx.xx.xxx",
                "tomcat_statuses":{
                    "checkdb":"Failed",
                    "checkhub":"couldnt connect to host"
                }
                }

            ]
        }
    ]
}', true);

$mergedarray = array_replace_recursive($arr1, $arr2);
print '<pre>';
print_r($mergedarray);
print '</pre>';
exit;
  • 结果 *
Array
(
    [PatchDeployment] => Array
        (
            [0] => Array
                (
                    [ehubid] => 123
                    [tomcats] => Array
                        (
                            [0] => Array
                                (
                                    [tomcatname] => mytomcat
                                    [servername] => myserver
                                    [serverip] => xx.xx.xx.xxx
                                    [tomcat_statuses] => Array
                                        (
                                            [stoptomcat] => Success
                                            [checkdb] => Failed
                                            [checkhub] => couldnt connect to host
                                        )

                                )

                        )

                )

        )

)
vaqhlq81

vaqhlq812#

尝试使用array_merge_recursive

$mergedArr = array_merge_recursive($arr1, $arr2);

相关问题