PHP - array_diff_key
Trieda
Metóda - array_diff_key
(PHP 5 >= 5.1.0, PHP 7)
The function compares the keys of the arrays and returns the difference.
Note: The function doesn't work recursively for multidimensional arrays.
Procedurálne
- function array_diff_key (array $array1, array $array2, array $...) : array
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $array1 | array | The keys of the array are compared to the keys in the following arrays. If the key is a string, the comparison is case sensitive. | |
| $array2 | array | The array which keys are compared to the keys in the first array. | |
| $... | array | More arrays which keys are compared to the keys in the first array. |
Mávratovej hodnoty
Vracia: array
The function returns an array containing the elements from the first array which keys are not present in any other array.
Príklady
In the first example we have two associative arrays and compare their keys:
<?php
$pole1 = array("firstName" => "Chuck", "lastName" => "Norris", "age" => 30, "movie" => "The Delta Force");
$pole2 = array("age" => 50, "movie" => "Rambo 1");
print_r(array_diff_key($pole1, $pole2));
echo "<br>";
// If the key is a string, the comparison is case sensitive:
$pole2 = array("age" => 50, "MOVIE" => "Rambo 1");
print_r(array_diff_key($pole1, $pole2));
In the second example we have two indexed arrays and compare their keys:
<?php
$pole1 = array("BMW", "Mercedes", "Audi", "Volvo", "Subaru", "Lexus");
$pole2 = array(4 => "Honda", "Mazda", "VW");
print_r(array_diff_key($pole1, $pole2));
In the third example we have three different arrays:
<?php
$array1 = array("a" => "red", "b" => "blue", "c" => "yellow", "d" => "green", "e" => "white");
$array2 = array("a" => "Vovo", "d" => "Audi");
$array3 = array("b" => 1789, "d" => 2017);
print_r(array_diff_key($array1, $array2, $array3));
Súvisiace manuály
- function array_intersect (array $array1, array $array2, array $...) : array
- function array_intersect_key (array $array1, array $array2, array $...) : array
