Practical tips for PHP functions: array_intersect()

PHPz
Release: 2023-06-20 08:40:01
Original
3954 people have browsed it

In PHP, array_intersect() is a very useful array function that can be used to compare identical elements between two or more arrays and return the array intersection of these elements. This article will introduce the basic usage of the array_intersect() function and some practical tips to help PHP developers make better use of this function.

1. Function syntax and basic usage

array_intersect() The syntax of the function is as follows:

array_intersect ( array $array1 , array $array2 [, array $... ] ) : array
Copy after login

This function requires at least two arrays as parameters, but can accept multiple arrays . The return value of the function is a new array containing the intersection elements of all arrays. For example:

$arr1 = array('apple', 'banana', 'pear');
$arr2 = array('banana', 'orange', 'grape');
$arr3 = array('banana', 'kiwi', 'pear');
$result = array_intersect($arr1, $arr2, $arr3);
print_r($result); //输出结果为:array('banana');
Copy after login

In this example, we define 3 arrays: $arr1, $arr2 and $arr3, each containing some fruit names. Use the array_intersect($arr1, $arr2, $arr3) function to calculate the intersection of these arrays and print the return result. It turns out that the only intersection element in these arrays is "banana".

2. Practical skills

In addition to basic usage, the array_intersect() function also has some practical skills, which will be introduced one by one below.

  1. Remove duplicate elements

If we have an array that may have some duplicate elements, we can use the array_unique() function to remove these duplicate elements. However, if we want to keep only duplicate elements and remove other elements, we can use array_intersect() function. For example:

$arr1 = array('apple', 'banana', 'orange', 'pear', 'banana', 'kiwi');
$arr2 = array_unique($arr1); //去除重复元素
$result = array_intersect($arr1, $arr2); //返回重复元素
print_r($result); //输出结果为:array('banana');
Copy after login

In this example, we define an array $arr1 containing duplicate elements, first use the array_unique() function to remove these duplicate elements, and then use the array_intersect() function to calculate the values ​​of $arr1 and $arr2 Intersection, the result contains only the repeated element "banana" in the array.

  1. Compare the intersection of multiple arrays

array_intersect() function can accept multiple arrays as parameters, which provides convenience for us to compare the intersection of multiple arrays. For example, we can use array_intersect($arr1, $arr2, $arr3) to calculate the intersection between 3 arrays, but what if we have multiple arrays to compare? This can be achieved using a for loop, as shown below:

$arr1 = array('apple', 'banana', 'pear');
$arr2 = array('banana', 'orange', 'grape');
$arr3 = array('banana', 'kiwi', 'pear');
$arr4 = array('banana', 'cherry', 'pear');
$cnt = count(func_get_args()); //获取参数个数
for ($i = 1; $i < $cnt; $i++) {
    ${"arr" . $i} = array_intersect(${"arr" . ($i-1)}, ${"arr" . $i}); //比较数组交集
}
print_r($arr1); //输出结果为:array('banana');
Copy after login

In this example, we define 4 arrays: $arr1, $arr2, $arr3 and $arr4, each containing some fruit names. Using a for loop, we compare the intersection of these arrays and store the results in the same array. Finally, we print the array containing the intersection elements, and the result contains only "banana". This example illustrates the convenience of the array_intersect() function when comparing multiple arrays.

  1. Keep the original array key names

By default, the array_intersect() function will keep the key names of the original array. But if we want to force the key name to be reset, we can use the array_values() function. For example:

$arr1 = array('name' => 'Tom', 'age' => 32);
$arr2 = array('name' => 'Jerry', 'age' => 25);
$arr3 = array_intersect($arr1, $arr2);
print_r($arr3); //输出结果为:array('name' => 'Jerry', 'age' => 25);
$arr4 = array_values(array_intersect($arr1, $arr2));
print_r($arr4); //输出结果为:array('Jerry', 25);
Copy after login

In this example, we define two associative arrays $arr1 and $arr2, which contain some basic information. Use the array_intersect() function to compare the intersection of the two arrays and preserve the original key names, and then use the print_r() function again to print the result. The output now contains the original key names "name" and "age". Next, we use the array_values() function to strip off the original keys, leaving only the intersection elements and print the result. At this time, the output result only contains the intersection elements "Jerry" and "25", indicating that the array_values() function has removed the original key names.

  1. Compare the intersection of multi-dimensional arrays

The array_intersect() function is also suitable for comparison of multi-dimensional arrays. For example, we can use array_intersect($arr1, $arr2, $arr3) to compare the intersection of multiple one-dimensional arrays, or we can use array_intersect($arr1[0], $arr2[0], $arr3[0]) to Compares the intersection of multidimensional arrays. For example:

$arr1 = array(
    array('name' => 'Tom', 'age' => 32),
    array('name' => 'Jerry', 'age' => 25),
    array('name' => 'Mickey', 'age' => 28)
);
$arr2 = array(
    array('name' => 'Jerry', 'age' => 25),
    array('name' => 'Mickey', 'age' => 28),
    array('name' => 'Donald', 'age' => 30)
);
$arr3 = array(
    array('name' => 'Tom', 'age' => 32),
    array('name' => 'Jerry', 'age' => 25),
    array('name' => 'Mickey', 'age' => 28)
);
$arr4 = array_intersect($arr1[0], $arr2[0], $arr3[0]);
print_r($arr4); //输出结果为:array('name' => 'Jerry', 'age' => 25);
Copy after login

In this example, we define 4 multidimensional arrays: $arr1, $arr2, $arr3 and $arr4. Use the array_intersect() function to compare the intersection of these arrays, and the result only contains information about "Jerry". This example illustrates that the array_intersect() function is suitable for comparison of multi-dimensional arrays and the syntax is very simple.

3. Summary

The array_intersect() function is very practical in PHP development. It can easily compare the intersections between multiple arrays and return repeated elements. This article introduces the basic usage of this function and some practical skills, including removing duplicate elements, comparing the intersection of multiple arrays, retaining the original array key names, and comparing the intersection of multi-dimensional arrays. These tips can help PHP developers make better use of the array_intersect() function and improve development efficiency and code quality.

The above is the detailed content of Practical tips for PHP functions: array_intersect(). For more information, please follow other related articles on the PHP Chinese website!

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