Php array intersect uassoc
From w3cyberlearnings
Contents |
PHP function array_intersect_uassoc
This function compares two or more arrays by using the user-defined function. When the function doing the comparison, the function uses the user-defined function calls the first array key and value to compare with other arrays. It returns the array that match based on the user-defined function.
Syntax array_intersect_uassoc
- array1: array 1 uses for the comparison with all other arrays
- array2: second array
- array3: third array
- callback: user-defined function.
array_intersect_uassoc(array1, array2, array3, ...,callback);
Example 1
<?php function callback($v1, $v2) { if ($v1 == $v2) { return 0; } else if ($v1 > $v2) { return 1; } else { return -1; } } $play1 = array(30=>'bob', 40=>'jame', 50=>'smit', 20=>'king'); $play2 = array(30=>'bob', 42=>'smit', 40=>'jame', 32=>'king'); $play3 = array(40=>'jame', 30=>'bob', 1=>'king', 3=>'smit'); print_r(array_intersect_uassoc($play1, $play2, $play3, "callback")); ?>
Output
Array ( [30] => bob [40] => jame )
Example 2
<?php $class1 = array('A' => 13, 'B' => 31, 'cC' => 12, 'D' => 40); $class2 = array('a' => 13, 'B' => 31, 'CC' => 12, 'D' => 40); $class3 = array('A' => 13, 'b' => 31, 'cc' => 12, 'D' => 30); $classes = array_intersect_uassoc($class1, $class2, $class3, "strcasecmp"); print_r($classes); ?>
Output
Array ( [A] => 13 [B] => 31 [cC] => 12 )
Example 3
<?php function callback($vl1, $vl2) { if ($vl1 > $vl2) { return 0; } else { return -1; } } $p1 = array(14 => "banana", 1 => "apple", 12 => "coconut"); $p2 = array(4 => "banana", 1 => "apple", 5 => "coconut"); $p = array_intersect_uassoc($p1, $p2, "callback"); foreach($p as $n => $fruit ) { echo "{$fruit} contains {$n}, and it is the largest.<br/>"; } ?>
Output
banana contains 14, and it is the largest. coconut contains 12, and it is the largest.
Example 4
<?php function callback($vl1, $vl2) { if ($vl1 < $vl2) { return 0; } else { return -1; } } $p1 = array(14 => "banana", 341 => "apple", 12 => "coconut"); $p2 = array(4 => "banana", 12 => "apple", 52 => "coconut"); $p = array_intersect_uassoc($p1, $p2, "callback"); foreach($p as $n => $fruit ) { echo "{$fruit} contains {$n}, and it is the smallest.<br/>"; } ?>
Output
coconut contains 12, and it is the smallest.
Related Links
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys