Do multiple php arrays have the same elements?

(*-*)浩
Release: 2023-02-26 13:00:01
Original
2725 people have browsed it

Do multiple php arrays have the same elements?

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 $...])
Copy after login

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(&#39;1&#39;, &#39;a&#39; => &#39;aaaaaa&#39;, &#39;b&#39; => &#39;bbbbbb&#39;, &#39;c&#39;);
$array2 = array(&#39;a&#39; => &#39;aaaaaa&#39;, &#39;c&#39; => &#39;bbbbbb&#39;, &#39;c&#39;, &#39;1&#39;);
 
var_dump(array_intersect($array1,$array2));
Copy after login

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"
}
Copy after login

and use The method array_intersect_assoc() will get the following results:

array(1) {
  ["a"]=>
  string(6) "aaaaaa"
}
Copy after login

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!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!