PHP array_diff_key() function

P
PHP array_diff_assoc() function
PHP array_diff_uassoc() function

The array_diff_key function simply compares two or more arrays based on their keys. This function compares the keys of the first array given as the parameter with other arrays and returns an array containing the difference.

What is the syntax of array_diff_key function in PHP?

array_diff_key(array1, array2, array3, ...)
ParametersDescription
array1The first array to compare with other arrays – required
array2Second array to make a comparison against- required
array3,…Further arrays to compare against – optional
array_diff_key function in PHP

Examples of array_diff_key function

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");

$result=array_diff_key($a1,$a2);
print_r($result);
?>

Example of array_diff_key with indexed arrays – How to find the difference between associative arrays in PHP?

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");

$result=array_diff_key($a1,$a2);
print_r($result);
?>

Example of array_diff_key function with more than two arrays

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("c"=>"yellow","d"=>"black","e"=>"brown");
$a3=array("f"=>"green","c"=>"purple","g"=>"red");

$result=array_diff_key($a1,$a2,$a3);
print_r($result);
?>
PHP array_diff_assoc() function
PHP array_diff_uassoc() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z