Current Location: Home> Latest Articles> How to effectively process JSON data in combination with the json_decode() and array_diff_key() functions and filter out the specified key-value pairs?

How to effectively process JSON data in combination with the json_decode() and array_diff_key() functions and filter out the specified key-value pairs?

M66 2025-05-15

In PHP, processing JSON data is a common task, especially when we need to fetch or transfer data from the API. One of the common requirements is to filter out the specified key-value pairs in JSON, which can usually be processed by converting JSON to a PHP array and then using the corresponding functions. This article will introduce how to combine json_decode() and array_diff_key() functions to effectively filter out specific key-value pairs in JSON data.

1. Use json_decode() function to parse JSON data

First, the json_decode() function is used to convert strings in JSON format to PHP arrays. The basic usage of this function is as follows:

 $json_data = '{"name": "John", "age": 30, "city": "New York"}';
$data = json_decode($json_data, true);  // The second parameter true Let the return result be an array

In the above code, $json_data is a JSON string, which json_decode() converts to the PHP array $data . The converted array is:

 Array
(
    [name] => John
    [age] => 30
    [city] => New York
)

2. Use the array_diff_key() function to filter the specified key-value pairs

Once the JSON data is converted to an array, we can use the array_diff_key() function to filter out unwanted key-value pairs. This function compares the keys of two arrays and returns key-value pairs in the first array that are not in the second array.

Suppose we want to remove the age and city keys from the above array, leaving only the name key. We can do this:

 $keys_to_keep = ['name' => null];  // Keys to be retained
$filtered_data = array_diff_key($data, $keys_to_keep);

However, the array_diff_key() function returns an array with key-value pairs removed, so the code here may be confusing. To avoid misunderstanding, we can keep specific keys by reversely operating the keys to be deleted:

 $keys_to_remove = ['age' => null, 'city' => null];  // Keys to remove
$filtered_data = array_diff_key($data, $keys_to_remove);

In this way, $filtered_data will only contain the value corresponding to the name key:

 Array
(
    [name] => John
)

3. Practical application example: Get data from the API and filter it

Suppose we have some JSON data from the API and hope to filter out the parts we care about. Here is a more specific example:

 // Suppose we are from some URL Obtained JSON data
$json_url = "https://m66.net/api/user_data";  // Replace with actual URL
$json_data = file_get_contents($json_url);

// Will JSON data解码为 PHP Array
$data = json_decode($json_data, true);

// Assume we only need user_id and user_name These two fields
$keys_to_keep = ['user_id' => null, 'user_name' => null];

// use array_diff_key 筛选需要的data
$filtered_data = array_diff_key($data, $keys_to_keep);

// 输出筛选后的data
print_r($filtered_data);

In this example, the file_get_contents() function gets JSON data from the specified URL and converts it to a PHP array via json_decode() . Then, use array_diff_key() to filter out the required fields and finally print out the filtered data.

4. Summary

By combining the json_decode() and array_diff_key() functions, we can easily filter out specified key-value pairs from JSON data. json_decode() is responsible for converting JSON strings into PHP arrays, while array_diff_key() helps us remove unwanted keys to get some of the data we need. This approach can be widely used in processing API call results, as well as in any scenario where specific information is needed to extract from JSON data.

  • Related Tags:

    JSON