php returns different elements in two arrays

(*-*)浩
Release: 2023-02-26 12:56:01
Original
2818 people have browsed it

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

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.

<?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_assoc($array1, $array2));
Copy after login

Get the following results:

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

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!

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!