Jump to: navigation, search

Php array intersect ukey

From w3cyberlearnings

Contents

PHP function array_intersect_ukey

This function compares two or more arrays by using the user-defined function. The user-defined function uses the first array key and checks it with other arrays key for comparison. It returns the key and value pairs from the first array if the comparison within the user-defined function return true.

Syntax array_intersect_ukey

  • array1: input array for comparison with other arrays
  • array2: second array
  • array3: third array
  • callback: user-defined function uses for the comparison.
array_intersect_ukey(array1, array2, array3, ...,callback);

Example 1

<?php

function callback($v1, $v2) {
	if ($v1 > $v2) {
		return 0;
	} else {
		return -1;
	}
}

$p1 = array(1 => 'Great', 6 => 'Excellent', 3 => 'Wonderful');
$p2 = array(2 => 'Excellent', 3 => 'Great', 5 => 'Wonderful');


$p = array_intersect_ukey($p1, $p2, "callback");

foreach ($p as $digit => $word) {
	echo "{$word} contains {$digit} rating, and it is the greatest.<br/>";
}
?>


Output

Excellent contains 6 rating, and it is the greatest.

Example 2


<?php

function callback($v1, $v2) {
	if ($v1 < $v2) {
		return 0;
	} else {
		return -1;
	}
}

$p1 = array(1 => 'Great', 6 => 'Excellent', 3 => 'Wonderful');
$p2 = array(2 => 'Excellent', 3 => 'Great', 5 => 'Wonderful');


$p = array_intersect_ukey($p1, $p2, "callback");

foreach ($p as $digit => $word) {
	echo "{$word} contains {$digit} rating, and it is the smallest.<br/>";
}
?>


Output


Great contains 1 rating, and it is the smallest.
Wonderful contains 3 rating, and it is the smallest.

Example 3


<?php

function callback($v1, $v2) {
	if ($v1 == $v2) {
		return 0;
	} else if ($v1 > $v2) {
		return 1;
	} else {
		return -1;
	}
}

$p1 = array(1 => 'Great', 6 => 'Excellent', 3 => 'Wonderful');
$p2 = array(2 => 'Excellent', 1 => 'Great', 5 => 'Wonderful');


$p = array_intersect_ukey($p1, $p2, "callback");

foreach ($p as $digit => $word) {
	echo "{$word} contains {$digit} rating, and it is the same in all level.<br/>";
}
?>

Output

Great contains 1 rating, and it is the same in all level.

Related Links


Navigation
Web
SQL
MISC
References