English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriais PHP Básicos

Tutoriais PHP Avançados

PHP & MySQL

Manual de referência PHP

Uso e exemplo do PHP array_diff_uassoc()

PHP Array Function Manual

definição e uso

array_diff_uassoc()função compara chaves e valores de dois (ou mais) arrays e retorna um array contendo1do array de itens, que não existem em nenhum outro array com valores idênticos.

esta função é diferente dearray_diff()porque array_diff() compara valores, enquanto esta função compara chaves e valores de outros arrays.

esta função é semelhante aarray_diff_assoc() é diferente, pois array_diff_assoc() usa um algoritmo interno para comparar chaves e valores, enquanto esta função usa uma função definida pelo usuário para comparar chaves e valores.

sintaxe

array_diff_uassoc( $array1, $array2 , $array3..., callback $key_compare_func]);

Parameter

Serial NumberParameters and Description
1

array1(Required)

Array to compare

2

array2(Required)

This is the array to be compared with the first array

3

array3(Optional)

Array to compare with the first array

4

key_compare_func(Required)

The comparison function must return an integer less than, equal to, or greater than 0 when the first parameter is less than, equal to, or greater than the second parameter.

Return Value

This function returns an array containing the array1in all entries, but not in any other array.

PHP version

This function was first introduced in PHP version5.0.0 introduced.

Online Example

Try the following example. Here, if $input1If the key is equal to any other input array, the key comparison function returns 0; if larger, return1; If smaller, return -1.

When comparing keys with a defined function, both arrays indeed have the key "a", so it will not appear in the output. The next key "b" and "c" are not in the second array, so they will enter the output. Another pair 0 => "red" is in the output because the key for "red" in the second parameter is1:

<?php
   function key_compare_func($a, $b) {
      if ($a === $b) {
         return 0;
      }
      return ($a > $b)? 1: -1;
   }
   $input1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
   $input2 = array("a" => "green", "yellow", "red");
   $result = array_diff_uassoc($input1, $input2, "key_compare_func");
   print_r($result);
?>
Test and see‹/›

Output Result:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

Online Example

Try the following example. This time "red" will not be in the output because both keys are now equal to 0.

<?php
   function key_compare_func($a, $b) {
      if ($a === $b) {
         return 0;
      }
      return ($a > $b)? 1: -1;
   }
   $input1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
   $input2 = array("a" => "green", "c" => "yellow", "red");
   $result = array_diff_uassoc($input1, $input2, "key_compare_func");
   print_r($result);
?>
Test and see‹/›

Output Result:

Array
(
    [b] => brown
    [c] => blue
)

PHP Array Function Manual