Home > Backend Development > PHP Tutorial > PHP returns the difference array of two arrays

PHP returns the difference array of two arrays

王林
Release: 2024-03-21 09:40:01
forward
823 people have browsed it

php editor Xigua will introduce you how to use PHP to return the difference array of two arrays. A difference array refers to a new array formed by taking elements from the first array that are not in the second array. This functionality can be easily achieved by using the array_diff() function. Simply pass in two arrays as arguments and the function will return a new array containing the elements of the difference set. This method is very convenient and fast, and is suitable for dealing with various array operation problems.

How to use PHP to return the difference array of two arrays

In php, we can return the difference between two arrays by using the array_diff() function. This function accepts two arrays as arguments and returns a new array containing all elements in the first array that are not in the second array.

grammar:

array_diff(array1, array2);
Copy after login

parameter:

  • array1: The array from which elements are to be removed.
  • array2: The array in which the element to be deleted is located.

return value:

A new array containing all elements in array1 that are not in array2.

Example:

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

$diff = array_diff($array1, $array2);

print_r($diff); // Output: Array ( [0] => 1 [1] => 3 [2] => 5 )
Copy after login

In the above example, the array_diff() function returns a new array containing elements 1, 3, and 5 in array1 because these elements are not in array2 middle.

Custom comparison function:

We can use the array_udiff() function to customize the comparison function to determine which elements should be removed from the first array.

grammar:

array_udiff(array1, array2, callback);
Copy after login

parameter:

  • array1: The array from which elements are to be removed.
  • array2: The array in which the element to be deleted is located.
  • callback: A user-defined comparison function that accepts two elements as parameters and returns -1 (if the first element should be deleted), 0 (if the two elements are equal) or 1 if the second element should be removed.

Example:

$array1 = [
["name" => "John", "age" => 30],
["name" => "Mary", "age" => 25],
["name" => "Bob", "age" => 40],
];

$array2 = [
["name" => "Mary", "age" => 25],
];

$diff = array_udiff($array1, $array2, function ($a, $b) {
return strcmp($a["name"], $b["name"]);
});

print_r($diff); // Output: Array ( [0] => Array ( [name] => John [age] => 30 ) [1] => Array ( [name] => Bob [age] => 40 ) )
Copy after login

In the above example, we use the array_udiff() function to compare the name attribute of the objects in the array. It returns a new array containing all name properties in array1 and different objects in array2.

Notice:

    The
  • array_diff() and array_udiff() functions will only compare elements with the same index in the array.
  • If two arrays have different keys, these keys will be ignored and only elements with the same key will be compared.
  • We can use the array_keys() or array_values() function to get a list of all keys or values ​​of an array so that we can compare arrays based on different criteria.

The above is the detailed content of PHP returns the difference array of two arrays. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
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