How to merge below array in PHP?

81 Views Asked by At

I have two json format where I am converting that json into array and then merge it, but its not working.

see below two json format.

Below is my code in PHP and its not merging the array, what wrongs here ?

$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);

I want array should be like below after merging $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
                                        )

                                )

                        )

                )

        )

)

I am merging two array into php

Anyone can help me to sort out this issue as soon as possible?

2

There are 2 best solutions below

0
cengsemihsahin On BEST ANSWER

You don't want them to repeat. The method you are looking for: array_replace_recursive
Code

$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;

Result

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
                                        )

                                )

                        )

                )

        )

)
0
protob On

Try to use array_merge_recursive:

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