Current Location: Home> Latest Articles> How to make array_diff() compare JSON decoded arrays?

How to make array_diff() compare JSON decoded arrays?

M66 2025-05-14

In daily PHP development, we often need to compare the differences between two arrays, especially when these arrays are parsed from JSON data. The array_diff() function can help us find elements that exist in the first array but not in the second array. This article will explain how to use array_diff() correctly when processing JSON data.

1. Basic usage of array_diff()

PHP's array_diff() function is used to compare two or more arrays and return values ​​that exist in the first array but not in other arrays:

 $array1 = ["a", "b", "c", "d"];
$array2 = ["b", "d"];

$result = array_diff($array1, $array2);
print_r($result);
// Output:Array ( [0] => a [2] => c )

2. Process JSON data and compare

Usually we will get JSON format data from the API or external files, convert it into an array using json_decode() , and then use array_diff() for comparison.

Example: Comparing the differences between two JSON strings

 $json1 = '["apple", "banana", "cherry", "date"]';
$json2 = '["banana", "date", "fig"]';

// Decode into an array
$array1 = json_decode($json1, true);
$array2 = json_decode($json2, true);

// Comparison of the differences
$diff = array_diff($array1, $array2);

print_r($diff);
// Output:Array ( [0] => apple [2] => cherry )

3. Things to note

  1. Keep the data structure consistent : json_decode() returns the object by default, and the second parameter needs to be set to true to obtain the associative array or index array.

  2. The key name does not affect the result : array_diff() only compares the values, and does not consider the key name.

  3. The data type should be consistent : if an array is an integer and the other is a number in the form of a string (such as "1" vs 1 ), it may cause inaccuracy. You can use array_map() to unify the type:

 $array1 = array_map('strval', $array1);
$array2 = array_map('strval', $array2);

4. Practical application scenarios

Suppose you are synchronizing data from two systems, one is JSON data provided by the remote API, and the other is records from the local database. You can find out the new projects you need to add in the following ways:

 $remoteJson = file_get_contents('https://api.m66.net/data/items');
$remoteData = json_decode($remoteJson, true);

$localData = ["item_101", "item_102", "item_103"]; // From the database ID List

$newItems = array_diff($remoteData, $localData);

print_r($newItems);
// Output:Remote but locally not item ID List

5. Summary

Using array_diff() combined with json_decode() can compare the differences in JSON data very efficiently. It is very useful in handling data synchronization, data verification, data change prompts and other scenarios. Remember to pay attention to the unity of data structure and type when using it in actual use to avoid inaccurate situations.

If you are developing similar functions, it is recommended to encapsulate this part of the logic into a function or service class to improve code reusability and maintainability.