Home  >  Article  >  Backend Development  >  php returns different elements in two arrays

php returns different elements in two arrays

(*-*)浩
(*-*)浩Original
2019-10-12 13:17:142702browse

php returns different elements in two arrays

Get different elements in the array

php compares different elements in two arrays (recommended learning: PHP video Tutorial)

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

Similarly, the basic functions of these two methods are the same, 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.

array_diff() function returns the difference array of two arrays. This array contains all keys that are in the array being compared, but are not in any of the other parameter arrays.

In the returned array, the key names remain unchanged.

 'aaaaaa', 'b' => 'bbbbbb', 'c');
$array2 = array('a' => 'aaaaaa', 'c' => 'bbbbbb', 'c', '1');
 
var_dump(array_diff_assoc($array1, $array2));

Get the following results:

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

The above is the detailed content of php returns different elements in two arrays. 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