php Gets the same elements in multiple arrays (Recommended learning: PHP video tutorial)
array array_intersect(array $array1, array $array2, [, array $...]) array array_intersect_assoc(array $array1, array $array2, [, array $...])
This The functions of the two methods are basically the same. They both return elements that exist in both arrays (or multiple arrays). The difference is that the former only considers the values of the elements in the arrays to be the same if they are consistent, while the latter requires Both key and value are considered to be the same if they are consistent , for example:
<?php $array1 = array('1', 'a' => 'aaaaaa', 'b' => 'bbbbbb', 'c'); $array2 = array('a' => 'aaaaaa', 'c' => 'bbbbbb', 'c', '1'); var_dump(array_intersect($array1,$array2));
Running the above code will get the following results:
array(4) { [0]=> string(1) "1" ["a"]=> string(6) "aaaaaa" ["b"]=> string(6) "bbbbbb" [1]=> string(1) "c" }
and use The method array_intersect_assoc() will get the following results:
array(1) { ["a"]=> string(6) "aaaaaa" }
The above is the detailed content of Do multiple php arrays have the same elements?. For more information, please follow other related articles on the PHP Chinese website!