gnìomh PHP array_diff_key

SSL earbsach bho dìreach $3.44
sanasan
gnìomh PHP array_diff_assoc().
gnìomh PHP array_diff_uassoc().

In PHP, the array_diff_key() dòigh compares two or more arrays and returns an array containing the differences depending on the keys. This method is useful for comparing arrays and determining which keys are present in one array but not in others.

The array diff key() method has the following syntax:

What is the syntax of the array_diff_key function in PHP?

array array_diff_key ( array $array1 , array $array2 [, array $... ] )
ChrìochanTuairisgeul
eagar 1Tha a 'chiad sreath gus coimeas a dhèanamh le arrays eile - a dhìth
eagar 2An dàrna sreath gus coimeas a dhèanamh an aghaidh a tha a dhìth
sreath 3,…Barrachd arrays airson coimeas a dhèanamh riutha - roghainneil
gnìomh array_diff_key ann am PHP

The first array to be compared is specified by the $array1 argument, while the second array is specified by the $array2 parameter. Additional arrays can be passed as arguments and are compared to $array1.

To understand how the array diff key() method works, consider the following examples:

Example 1:

$array1 = array("red" => "apple", "green" => "banana", "blue" => "cherry");
$array2 = array("red" => "apple", "yellow" => "lemon");
$diff = array_diff_key($array1, $array2);
print_r($diff);

Toraidhean:

Array
(
    [green] => banana
    [blue] => cherry
)

$array1 and $array2 are compared in this example, and the $diff array includes the keys that are present in $array1 but not in $array2.

Example 2:

$array1 = array("red" => "apple", "green" => "banana", "blue" => "cherry");
$array2 = array("red" => "apple", "yellow" => "lemon");
$array3 = array("green" => "kiwi", "pink" => "grapefruit");
$diff = array_diff_key($array1, $array2, $array3);
print_r($diff);
SSL earbsach bho dìreach $3.44
sanasan

Toraidhean:

Array
(
    [blue] => cherry
)

$array1, $array2, and $array3 are compared in this example, and the $diff array gabhail a-steach the keys that are present in $array1 but not in $array2 or $array3.

Example 3:

$array1 = array("red" => "apple", "green" => "banana", "blue" => "cherry");
$array2 = array("red" => "apple", "yellow" => "lemon");
$array3 = array("green" => "banana", "pink" => "grapefruit");
$diff = array_diff_key($array1, $array2, $array3);
print_r($diff);

Toraidhean:

Array
(
    [blue] => cherry
)

Because the keys “green” are included in all three arrays in this example, the $diff array does not contain this key.

Finally, the array diff key() method in PHP is a valuable tool for comparing arrays and determining differences depending on the keys. This method may be used to compare two or more arrays and return an array containing the keys that are present in one but not the others. The syntax is simple, and the function is simple to use. The examples in this article will help you understand how the array diff key() method works and how it may be utilized in real-world applications.

It is vital to notice that the array diff key() method only compares the keys and not the values. If you need to compare both the keys and the values, use the array diff() method instead. It’s also worth noting that the order of the arrays being compared is unimportant.

Overall, the array diff key() method is useful for comparing arrays and determining differences depending on the keys. It’s a basic yet powerful tool that every PHP coder should be familiar with.

gnìomh PHP array_diff_assoc().
gnìomh PHP array_diff_uassoc().
dùin

Cùm suas ris an fhiosrachadh as ùire mu PHP!

Cha bhith sinn a’ spamadh!

SSL earbsach bho dìreach $3.44
sanasan
en English
X
Rach gu mhullaich