Home  >  Article  >  Backend Development  >  How to compare two different arrays in PHP

How to compare two different arrays in PHP

PHPz
PHPzOriginal
2023-04-24 17:12:03641browse

In PHP, arrays are a very important data type, they allow a group of related data items to be stored in the same variable. Using arrays can greatly improve the readability and maintainability of your code. However, in actual programming, it is often necessary to operate on arrays, such as finding the difference between two arrays. This article will explore how to compare two different arrays in PHP.

First, let's create two different arrays to better demonstrate how to compare them:

$array1 = array("apple", "banana", "orange", "pear");
$array2 = array("banana", "orange", "grapefruit");

Let's look at the differences between these two arrays. We can use the PHP built-in function array_diff() to find the differences between two arrays. The syntax of this function is as follows:

array_diff(array1, array2, array3...)

Among them, array1, array2, array3, etc. are the arrays to be compared, and there can be multiple arrays.

Now, let us use the array_diff() function to find the difference between $array1 and $array2. The code is as follows:

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

The output result is as follows:

Array
(
    [0] => apple
    [3] => pear
)

We can see that the output result is the difference between the two arrays. "apple" and "pear" in $array1 are in Does not exist in $array2.

What if we want to find the difference between $array2 and $array1? We only need to exchange the order of the arrays in the above code. The code is as follows:

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

The output result is as follows:

Array
(
    [2] => grapefruit
)

We can see that the output result is the difference between the two arrays. "grapefruit" in $array2 is not in $array1 exist.

In actual programming, you may encounter the situation of comparing the differences between multiple arrays. At this point, we can use a variant of PHP's built-in function array_diff(): array_diff_key() to compare key-value pairs between multiple arrays.

The syntax is as follows:

array_diff_key(array1, array2, array3...)

Now, let’s look at an example. We create three arrays as follows:

$array1 = array("name" => "Tom", "age" => 20, "gender" => "male");
$array2 = array("name" => "Jack", "age" => 22, "gender" => "male", "hobby" => "swimming");
$array3 = array("name" => "Alice", "age" => 24, "gender" => "female", "hobby" => "reading");

We can use the array_diff_key() function to find the differences between the above three arrays. The code is as follows:

$diff = array_diff_key($array1, $array2, $array3);
print_r($diff);

The output result is as follows:

Array
(
    [name] => Tom
    [age] => 20
    [gender] => male
)

We can see that the output result is the difference between the three arrays. The key-value pairs in $array1 are in $array2 and $ Does not exist in array3.

Finally, let’s talk about what to do if you need to find the same elements in two arrays at the same time. At this time, we can use the PHP built-in function array_intersect(). The syntax of this function is as follows:

array_intersect(array1, array2, array3...)

Among them, array1, array2, array3, etc. are the arrays to be compared, and there can be multiple arrays.

Now, let’s look at an example. We create two arrays as follows:

$array1 = array("apple", "banana", "orange", "pear");
$array2 = array("banana", "orange", "grapefruit");

We can use the array_intersect() function to find the same elements between the above two arrays. The code is as follows:

$intersect = array_intersect($array1, $array2);
print_r($intersect);

The output result is as follows:

Array
(
    [1] => banana
    [2] => orange
)

We can see that the output result is the common element between the two arrays, namely "banana" and "orange".

The above is how to compare different PHP arrays. In actual programming, we need to choose which method to use for array calculations based on specific needs.

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

Statement:
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