Organizational Research By

Surprising Reserch Topic

Question:array_intersect_ukey:how to Computes the intersection of arrays using a callback function on the keys for comparison using php ?



asked Sep 13, 2013 in PHP by rajesh
edited Sep 12, 2013
+1 vote
55 views



Related Hot Questions

2 Answers

0 votes

 

$a=array(1=>'a',2=>'b',3=>'c'); 
$b=array(2=>'z',3=>'c',4=>'d'); 
$c=array_intersect_key($a,$b); 
print_r($c); 
?> 
 
Will output 
Array 
( 
    [2] => b 
    [3] => c 
) 
 
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

 

function key_compare_func($key1, $key2)
{
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}
 
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
 
var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
?>
The above example will output:
array(2) {
  ["blue"]=>
  int(1)
  ["green"]=>
  int(3)
}
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...