PHP array_uintersect() function

Example

Compare the key values of two arrays (using a user-defined function to compare key values) and return the intersection:

<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1 = array("a" => "red", "b" => "green", "c" => "blue");
$a2 = array("a" => "blue", "b" => "black", "e" => "blue");
$result=array_uintersect($a1, $a2, "myfunction");
print_r($result);
?>

Executar Exemplo

Definition and Usage

The array_uintersect() function is used to compare the key values of two (or more) arrays and returns the intersection.

Note:This function uses a user-defined function to compare key values.

This function compares the key values of two (or more) arrays and returns an intersection array that includes all elements in the compared arrays (array1)e também em qualquer outro array de parâmetros (array2 ou array3 etc.) as chaves.

Syntax

array_uintersect(array1,array2,array3...myfunction)
Parameters Description
array1 Required. The first array to be compared with other arrays.
array2 Required. The array to be compared with the first array.
array3,... Optional. Other arrays to be compared with the first array.
myfunction

Required. A string value that defines the callable comparison function.

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

Description

using a user-defined callback function myfunction to calculate the intersection of two or more arrays (i.e., array1 contains all array elements that also exist in any other array), and returns the resulting array.

Only compares key values, not key names, so "a"=>1 and "b"=>1 are considered equal.

myfunction A função especificada pelos parâmetros é usada para comparar se os elementos são iguais.myfunction A função tem dois parâmetros a serem comparados. Se o primeiro parâmetro for menor que o segundo, a função retorna um número negativo, se os dois parâmetros forem iguais, deve retornar 0, e se o primeiro parâmetro for maior que o segundo, retorna um número positivo.

Os nomes das chaves no array retornado permanecem inalterados.

Detalhes Técnicos

Retorno:

Retorna um array contendo todos os elementos que estão array1 em todos os outros arrays.

Retorna um array de interseção que inclui todos os elementos que estão em todos os arrays comparados (array1)e também em qualquer outro array de parâmetros (array2 ou array3 etc.) as chaves.

Versão PHP: 5+

Mais Exemplos

Exemplo 1

Comparar chaves e valores de três arrays (usando função personalizada do usuário para comparar chaves e valores) e retornar a interseção:

<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow");
$a2=array("A"=>"red","b"=>"GREEN","yellow","black");
$a3=array("a"=>"green","b"=>"red","yellow","black");
$result=array_uintersect($a1,$a2,$a3,"myfunction");
print_r($result);
?>

Executar Exemplo