Home  >  Article  >  Backend Development  >  How to find the same elements in two arrays in php

How to find the same elements in two arrays in php

藏色散人
藏色散人Original
2022-12-23 10:04:365107browse

php method to find the same elements in two arrays: 1. Create a php sample file; 2. Define two arrays with the same elements; 3. Use "array_intersect($array1,$array2)" or " array_intersect_assoc()" method can obtain the same elements of two arrays.

How to find the same elements in two arrays in php

The operating environment of this tutorial: windows10 system, PHP8.1 version, DELL G3 computer

php How to find that two arrays are the same Elements?

php gets the same elements of two arrays (intersection) and compares different elements in the two arrays (difference set)

(1)php gets two arrays Same element

  array  array_intersect(array  $array1, array $array2, [, array $...])    
  array  array_intersect_assoc(array  $array1, array $array2, [, array $...])

The functions of these 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 value of the elements in the array. If they are consistent, the two are considered to be the same, and the latter requires that the key and value are consistent to consider the two to be the same. 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 using the method array_intersect_assoc () will get the following results:

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

(2) PHP compares different elements in two arrays

  array   array_diff(array  $array1, array $array2, [, array $...])
  array   array_diff_assoc(array  $array1, array $array2, [, array $...])

Similar, the basic functions of these two methods It is also consistent, returning elements that are in the first array but not in other arrays. The former only compares values, while the latter compares both key and value.

<?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_diff($array1,$array2));

The running result of the above code is:

array(0) {
}

And if you replace the last line with var_dump(array_diff_assoc($array1, $array2)); you will get the following result:

array(3) {
  [0]=>
  string(1) "1"
  ["b"]=>
  string(6) "bbbbbb"
  [1]=>
  string(1) "c"
}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to find the same elements in two arrays in php. 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