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

How to compare two arrays to find different values ​​in PHP

PHPz
PHPzOriginal
2023-04-19 10:09:371319browse

In PHP, it is often necessary to perform comparison operations on arrays, especially to analyze and compare the differences between two arrays. This article will introduce how to use PHP to compare two arrays and find their different values.

1. Initialization of two arrays

Assume that the two arrays $arr1 and $arr2 need to be compared. We first initialize through the following method:

$arr1 = array("a", "b", "c", "d");
$arr2 = array("b", "d", "f", "g");

2. Comparison method of two arrays

  1. array_diff()

PHP built-in functionarray_diff() You can find the values ​​in the first array that are different from the rest of the array. We can compare the two arrays, namely:

$diff1 = array_diff($arr1, $arr2);
$diff2 = array_diff($arr2, $arr1);

where $diff1 represents the value in $arr1 that is different from $arr2, $diff2 represents $ A value in arr2 that is different from $arr1. The results of outputting the above two arrays are as follows:

$diff1 = ['a', 'c'];
$diff2 = ['f', 'g'];
  1. array_merge()

In addition, we can use the array_merge() function to merge the two arrays Merge, and then use the array_unique() function to remove duplicates, and you will get the different values ​​​​of the two arrays. The specific implementation is as follows:

$merge_arr = array_merge($arr1, $arr2);
$unique_arr = array_unique($merge_arr);
$diff_arr = array_diff($unique_arr, array_intersect($arr1, $arr2));

In the above code, array_intersect($arr1, $arr2) represents the intersection of $arr1 and $arr2, array_diff() function uses to find elements in $unique_arr that appear only once. Ultimately $diff_arr is the different value between the two arrays.

3. Optimization of comparison of two arrays

When the length of the array to be compared is very large, the above method may affect the efficiency of the program. In order to optimize the comparison speed, we can use the array_flip() function to flip one of the arrays so that the key-value pairs are reversed, and then compare. The specific implementation method is as follows:

$flip_arr2 = array_flip($arr2);
$diff1 = array_diff_assoc($arr1, $flip_arr2);
$diff2 = array_diff_assoc($arr2, array_flip($arr1));

Among them, the array_diff_assoc() function is used to compare whether the keys and values ​​in the two arrays are the same.

4. Result output

Finally, we can output the result through the following code:

echo 'arr1 与 arr2 的差异为: '.implode(',', $diff1).'<br>';
echo 'arr2 与 arr1 的差异为: '.implode(',', $diff2).'<br>';
echo '两个数组不同值为: '.implode(',',$diff_arr).'<br>';

The output result of the above code is as follows:

arr1 与 arr2 的差异为: a,c
arr2 与 arr1 的差异为: f,g
两个数组不同值为: a,c,f,g

Summary:

This article introduces the method of comparing two arrays in PHP, and optimizes it for situations where the array length is very large. I hope it will be helpful to PHP beginners and readers who are not familiar with array comparison operations.

The above is the detailed content of How to compare two arrays to find different values ​​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