我有两个json字符串,当我通过curl发送请求时,我已将该字符串转换为数组。
现在我的问题是如果我的字符串被更新,我想发送另一个请求。
我使用过array_diff,但它给了我错误"注意:数组到字符串转换"因为它的多维度。
我的json字符串是:
{"toy":["cycle","doll"],"accessory":["tv"]}
,第二个是
{"toy":["cycle","cat","rabit"],"accessory":["tv","spekers"]}
如果不在另一个数组中,我如何区分这个和获取值。
我转换后的两个数组是这样的
Array
(
['toy'] => Array
(
[0] => cycle,
[1] => doll
),
['accessory'] => Array
(
[0] => tv
)
);
Array
(
['toy'] => Array
(
[0] => cycle,
[1] => cat,
[2] => rabit
),
[accessory] => Array
(
[0] => tv,
[1] => spekers
)
);
答案 0 :(得分:1)
这是我找到的解决方案
public function arrayRecursiveDiff($aArray1, $aArray2) {
$aReturn = array();
foreach ($aArray1 as $mKey => $mValue) {
if (array_key_exists($mKey, $aArray2)) {
if (is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
} else {
if ($mValue != $aArray2[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
return $aReturn;
}