Computer >> Computer tutorials >  >> Programming >> PHP

array_diff_key() function in PHP


The array_diff_key() function compares array keys, and returns the differences. It returns an array containing the entries from the first array that are not present in any of the other arrays.

Syntax

array_diff_key(arr1, arr2, arr3, arr4, ...)

Parameters

  • arr1 − Array to compare from. Required.

  • arr2 − Array to compare against. Required.

  • arr3 − You can add more arrays to compare. Optional.

  • arr4 − You can add more arrays to compare. Optional.

Return

The array_diff_key() function returns an array containing the entries from the first array that are not present in any of the other arrays.

Example

<?php
   $arr1 = array('car' => 10, 'bus' => 15, 'truck' => 22);
   $arr2 = array('motorbike' => 35, 'bus' => 42);
   $res = array_diff_key($arr1,$arr2);
   print_r($res);
?>

Output

Array
(
   [car] => 10
   [truck] => 22
)