Jump to: navigation, search

Php array diff key

From w3cyberlearnings

Contents

PHP function array_diff_key

This function compares two or more arrays, and the comparison is based on the first array and it uses array key for the comparison. It returns the array element in the first array that is not found in other arrays.

Syntax array_diff_key

  • array1: array that other arrays used for the comparsion
  • array2: second array
  • array3: third array
array_diff_key(array1, array2, array3, ...);

Example 1

<?php

$score1 = array(50 => 'China', 49 => 'US', 43 => 'Vietnam', 44 => 'Mexico');
$score2 = array(50 => 'Mexico', 30 => 'Canana', 43 => 'China', 43 => 'US');

$aa_diff = array_diff_key($score1, $score2);
print_r($aa_diff);
?>

Output

Array ( [49] => US [44] => Mexico )

Example 2

<?php

$score1 = array(50 => 'China', 49 => 'US', 43 => 'Vietnam', 44 => 'Mexico');
$score2 = array(50 => 'Mexico', 30 => 'Canana', 43 => 'China', 43 => 'US');
$aa_diff = array_diff_key($score1, $score2);
foreach ($aa_diff as $score => $country) {
	echo "The second team did not score {$score} as the players from {$country}<br/>";
}
?>

Output

The second team did not score 49 as the players from US
The second team did not score 44 as the players from Mexico

Related Links


Navigation
Web
SQL
MISC
References