1

以下のような同じキーを持つ 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

ありがとう!

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 に答える