2个不同的JSON数组,具有相同的键,如下所示:
数组1:
{ "fruit1" : "Apple", "fruit2" : "Banana", "fruit3" : "Grapes" }
数组2:
{ "fruit1" : "First Fruit", "fruit2" : "Second Fruit", "fruit3" : "Third Fruit" }
正如你所看到的,2个具有相同键但值不同的数组,现在我想要的是我希望结果或显示如下:
First Fruit is Apple
Second Fruit is Banana
Third Fruit is Grapes
谢谢!
答案 0 :(得分:3)
$str1 = '{ "fruit1" : "Apple", "fruit2" : "Banana", "fruit3" : "Grapes" }';
$str2 = '{ "fruit1" : "First Fruit", "fruit2" : "Second Fruit", "fruit3" : "Third Fruit" }';
$array1 = json_decode($str1);
$array2 = json_decode($str2);
foreach ($array1 as $key => $value) {
if (isset($array2[$key]) {
echo $value . " is " . $array2[$key];
} else {
echo $value . " has no match in array2.";
}
}
答案 1 :(得分:2)
$array1 = json_decode($json1, true);
$array2 = json_decode($json2, true);
foreach($array1 as $key => $val) {
if (isset($array2[$key])) {
echo $array1[$key],' is ',$array2[$key];
echo PHP_EOL;
}
}