How to calculate the difference of an array using PHP

WBOY
Release: 2024-03-14 08:02:01
Original
800 people have browsed it

How to calculate the difference of an array using PHP

Title: Using PHP to calculate the difference of arrays

In daily development work, it is often necessary to compare the differences between arrays and find out different elements. In PHP, we can use some functions and methods to quickly calculate the difference of arrays. This article will introduce you to some useful methods and specific code examples.

1. Use the array_diff function

The array_diff function in PHP is very convenient for calculating the difference between two arrays. It will return the values ​​that are in the first array but not in the other arrays.

$array1 = [1, 2, 3, 4, 5];
$array2 = [2, 3, 5, 6, 7];

$diff = array_diff($array1, $array2);
print_r($diff);
Copy after login

The above code will output:

Array
(
    [0] => 1
    [3] => 4
)
Copy after login
Copy after login

2. Use the array_diff_assoc function

If you need to calculate the difference of associative arrays, you can use the array_diff_assoc function, which compares the keys of the arrays name and key value.

$array1 = ['a' => 1, 'b' => 2, 'c' => 3];
$array2 = ['a' => 1, 'b' => 5, 'c' => 3];

$diff = array_diff_assoc($array1, $array2);
print_r($diff);
Copy after login

The output result is:

Array
(
    [b] => 2
)
Copy after login

3. Use custom functions

In addition to built-in functions, we can also write custom functions to calculate the difference of arrays, such as the following This example:

function array_diff_custom($array1, $array2) {
    $diff = [];
    foreach ($array1 as $key => $value) {
        if (!in_array($value, $array2)) {
            $diff[$key] = $value;
        }
    }
    return $diff;
}

$array1 = [1, 2, 3, 4, 5];
$array2 = [2, 3, 5, 6, 7];

$result = array_diff_custom($array1, $array2);
print_r($result);
Copy after login

The above code will output:

Array
(
    [0] => 1
    [3] => 4
)
Copy after login
Copy after login

Through the above three methods, we can easily calculate the difference between arrays and choose the most suitable method for comparison according to the actual situation. . I hope the above content is helpful to you, thank you for reading!

The above is the detailed content of How to calculate the difference of an array using 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!