PHP calculates the intersection of two arrays using two functions_PHP tutorial

WBOY
Release: 2016-07-20 11:02:53
Original
1168 people have browsed it

, using array_intersect to find the intersection of two arrays is faster than using array_diff to find the union of the same two arrays.

If you require the number of differences between array $a and array $b, you should use count($a) - count(array_intersect($a, $b)) instead of count(array_diff($a, $ b));


The former is faster than the latter, which is more obvious in large arrays.

The array_intersect() function returns the intersection array of two or more arrays.

The result array contains all values ​​in the compared array that also appear in all other parameter arrays, and the key names remain unchanged.

Note: Only values ​​are used for comparison.

Grammar
array_intersect(array1,array2,array3...) parameter description
array1 required. The first array to compare with other arrays.
array2 required. The array to compare to the first array.
array3 Optional. The array to compare to the first array. There can be multiple.
Example


$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_diff($a1,$a2));
?>Output:

Array ( [0] => Cat )


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445333.htmlTechArticle, using array_intersect to find the intersection of two arrays is faster than using array_diff to find the union of the same two arrays. If you require the number of differences between array $a and array $b, you should use...
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