Jump to: navigation, search

Php array diff ukey

From w3cyberlearnings

Contents

PHP function array_diff_ukey

This function compares two or more arrays by using the user-defined function, and the comparison 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_ukey

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

Example 1

<?php

function check_score($key1, $key2) {
	if ($key1 == $key2) {
		return 0;
	} else if ($key1 > $key2) {
		return 1;
	} else {
		return -1;
	}
}

$score1 = array('China'=>30,'Cambodia'=>32,'USA'=>31,'Thailand'=>32);
$score2 = array('Cambodia'=>31,'USA'=>32,'Vietnam'=>42,'China'=>32);

$aa_diff = array_diff_ukey($score1, $score2,"check_score");
print_r($aa_diff);
?>

Output

  • Thailand does not contain in the second array.

Array ( [Thailand] => 32 )

Example 2

<?php

function check_score($key1, $key2) {
	if ($key1 == $key2) {
		return 0;
	} else if ($key1 > $key2) {
		return 1;
	} else {
		return -1;
	}
}

$score1 = array('China'=>30,'Cambodia'=>32,'USA'=>31,'Thailand'=>32);
$score2 = array('Cambodia'=>31,'USA'=>32,'Vietnam'=>42,'China'=>32);

$aa_diff = array_diff_ukey($score1, $score2,"check_score");
foreach ($aa_diff as $country=>$score) {
	echo "The team from {$country} scores {$score} the first play, but missed all in the second play<br/>";
}
?>

Output

The team from Thailand scores 32 the first play, but missed all in the second play

Related Links


Navigation
Web
SQL
MISC
References