skip to Main Content

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

Answers


  1. Try to use array_merge_recursive:

    $mergedArr = array_merge_recursive($arr1, $arr2);
    
    Login or Signup to reply.
  2. 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
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search