Home > Backend Development > PHP Problem > How to get different parts of two associative arrays in php

How to get different parts of two associative arrays in php

PHPz
Release: 2023-03-23 15:46:01
Original
1319 people have browsed it

In PHP development, we often encounter the need to compare two associative arrays and obtain their different parts. Below we will introduce several ways to achieve this functionality.

Method 1: Use the array_diff_assoc() function

The array_diff_assoc() function can be used to compare the key-value pairs of two associative arrays and return the values ​​in the first array All elements in but not other arrays, and retain key names and key values.

Code example:

$array1 = array("name"=>"张三", "age"=>22, "salary"=>3000);  
$array2 = array("name"=>"李四", "age"=>22, "salary"=>4000);  
$result = array_diff_assoc($array1, $array2);  
print_r($result);
Copy after login

The output result is:

Array
(
    [name] => 张三
    [salary] => 3000
)
Copy after login
Copy after login
Copy after login

As you can see, the result array only contains the keys named "name" and "salary" in array 1 value pairs because they do not appear in array 2.

Method 2: Use a custom function

Another implementation method is a custom function that loops through two arrays and compares their key-value pairs, part is added to the result array.

Code example:

function array_diff_custom($array1, $array2) {  
    $result = array();  
    foreach ($array1 as $key => $value) {  
        if (!array_key_exists($key, $array2) || ($array2[$key] != $value)) {  
            $result[$key] = $value;  
        }  
    }  
    return $result;  
}  
$array1 = array("name"=>"张三", "age"=>22, "salary"=>3000);  
$array2 = array("name"=>"李四", "age"=>22, "salary"=>4000);  
$result = array_diff_custom($array1, $array2);  
print_r($result);
Copy after login

The output result is:

Array
(
    [name] => 张三
    [salary] => 3000
)
Copy after login
Copy after login
Copy after login

Similarly, the result array only contains the key-value pairs with the key names "name" and "salary" in array 1 .

Method 3: Use the array_udiff_assoc() function

The array_udiff_assoc() function is similar to the array_diff_assoc() function, but it can use a user-defined callback function to compare keys value pair.

Code example:

function compareByKeyValue($a, $b) {  
    if ($a === $b) {  
        return 0;  
    }  
    return ($a > $b) ? 1 : -1;  
}  
$array1 = array("name"=>"张三", "age"=>22, "salary"=>3000);  
$array2 = array("name"=>"李四", "age"=>22, "salary"=>4000);  
$result = array_udiff_assoc($array1, $array2, "compareByKeyValue");  
print_r($result);
Copy after login

The output result is:

Array
(
    [name] => 张三
    [salary] => 3000
)
Copy after login
Copy after login
Copy after login

We define a callback function compareByKeyValue() to compare whether key-value pairs are the same. By passing the name of this function as the third argument to the array_udiff_assoc() function, we can achieve a similar effect to the array_diff_assoc() function.

Summary

This article introduces three methods to obtain different parts of two associative arrays in PHP: using the array_diff_assoc() function, custom functions and array_udiff_assoc ()function. Which method you choose depends on specific needs and personal habits. Either way, you can improve efficiency and accuracy when working with associative arrays.

The above is the detailed content of How to get different parts of two associative arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template