Home  >  Article  >  Backend Development  >  Do multiple php arrays have the same elements?

Do multiple php arrays have the same elements?

(*-*)浩
(*-*)浩Original
2019-10-12 13:32:082728browse

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

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));

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!

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