English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
array_diff_assoc()função compara dois (ou mais) arrays e retorna a diferença.
esta função compara duas (ou mais) arrays de chaves e valores e retorna um array contendo1itens mas não no array2ou array3não existem itens no array...etc.
esta função é diferente dearray_diff()função, porque array_diff() usa apenas valores para comparar com outros arrays, enquantoarray_diff_assoc()The function uses both keys and values when comparing with other arrays.
array array_diff_assoc(array $array1, array $array2 [, array $array3]]);
Serial Number | Parameters and Description |
---|---|
1 | array1 (Required) The array to be compared |
2 | array2 (Required) It is the array to be compared with the first array |
3 | array3(Optional) It is the array to be compared with the first array |
The array_diff_assoc() function returns an array that contains the array1contains all values, which do not exist in any other array with the same key.
This function was first introduced in PHP version4.3introduced in version .0.
Try the following example. Both arrays contain “a” => “orange” and “c” => “banana”, so they will not appear in the result-
<?php $input1 = array("a" => "orange", "b" => "mango", "c" => "banana"); $input2 = array("a" => "orange", "b" => "apple", "c" => "banana"); print_r(array_diff_assoc($input1, $input2); ?>Test and see‹/›
Output Result:
Array ( [b] => mango )
In this case, the two arrays have different keys and corresponding values for all pairs, for example, "a" => "orange" does not exist in the second array, similarly, other key-value pairs also do not exist in the second array, so they will be available in the result-
<?php $input1 = array("a" => "orange", "b" => "mango", "c" => "banana"); $input2 = array("a" => "banana", "b" => "apple", "c" => "orange"); print_r(array_diff_assoc($input1, $input2); ?>Test and see‹/›
Output Result:
Array ( [a] => orange [b] => mango [c] => banana )
The following example illustrates that only when (string)$elem1 ===(string)$elem2only when the two values in the key=>value pair are considered equal.
<?php $input1 = array(0, 5, 20); $input2 = array("00", "05", "20); $result = array_diff_assoc($input1, $input2); print_r($result); ?>Test and see‹/›
Output Result:
Array ( [0] => 0 [1] => 5 )