我有两个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中
谁能帮我尽快解决这个问题?
2条答案
按热度按时间fhg3lkii1#
您不希望它们重复。您正在寻找的方法:
array_replace_recursive
代码
vaqhlq812#
尝试使用
array_merge_recursive
: