1

2 个具有相同键的不同 JSON 数组,如下所示:

阵列 1:

{ "fruit1" : "Apple", "fruit2" : "Banana", "fruit3" : "Grapes" }

阵列 2:

{ "fruit1" : "First Fruit", "fruit2" : "Second Fruit", "fruit3" : "Third Fruit" }

如您所见,两个具有相同键但值不同的数组,现在我想要的是我希望结果或显示如下:

First Fruit is Apple
Second Fruit is Banana
Third Fruit is Grapes

谢谢!

4

2 回答 2

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.";
    }
}
于 2012-06-27T15:42:14.983 回答
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;
  }
}
于 2012-06-27T15:42:42.403 回答