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
Try to use
array_merge_recursive
:You don’t want them to repeat. The method you are looking for:
array_replace_recursive
Code
Result