2016-04-07 3 views
-1

重複削除、2つのJSONオブジェクトを比較します。これらの2つのオブジェクトを比較し、今私は次のように2つのJSONオブジェクトを比較するエーブルすることが必要としています

$json1 = json_encode('["red", "green", "blue", "white"]'); 
$json2 = json_encode('["bla", "something", "haha", "blue"]'); 

を、「青」の値が重複している、とすべき$json2から削除してください。

どうすればいいですか?

答えて

1

あなたがPHPの配列json_decode($json1, true)にJSONをデコードしている場合:

は、重複を探す:

$dupes = array_intersect($json1, $json2); 

は(重複なし)の違いを取得:

//to remove from $json1 
$json1 = array_diff($json1, $dupes); 

//to remove from $json2 
$json2 = array_diff($json2, $dupes); 

だけ削除するには$json2よりはるかに簡単です:

$json2 = array_diff($json2, $json1); 
関連する問題