PHP array_intersect_ukey() Function
In this chapter you will learn:
- Definition for PHP array_intersect_ukey() Function
- Syntax for PHP array_intersect_ukey() Function
- Parameter for PHP array_intersect_ukey() Function
- Example - Intersect keys with user defined function
- Example - Compare the keys of three arrays using a user-defined function to compare the keys, and return the matches
Definition
The array_intersect_ukey() function compares the keys of two or more arrays, and returns the matches using a user-defined key comparison function.
Syntax
PHP array_intersect_ukey() Function has the following syntax.
array_intersect_ukey(array1,array2,array3...,myfunction)
Parameter
Parameter | Is Required | Description |
---|---|---|
array1 | Required. | Array compared with |
array2 | Required. | Array to be compared with array1 |
array3,... | Optional. | Array to be compared with array1 |
myfunction | Required. | User function to do the comparison |
myfunction defines a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument
Example
Intersect keys with user defined function
<?php//from j a v a 2s . co m
function myfunction($a,$b){
if ($a===$b){
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"A","b"=>"Bed","c"=>"Cat","j"=>"java2s.com");
$a2=array("d"=>"Dog","b"=>"Bed","p"=>"PHP");
$result=array_intersect_ukey($a1,$a2,"myfunction");
print_r($result);
?>
The code above generates the following result.
Example 2
Compare the keys of three arrays (use a user-defined function to compare the keys), and return the matches:
<?php/*from java 2 s . c om*/
function myfunction($a,$b){
if ($a===$b){
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"A","b"=>"Bed","c"=>"Cat","j"=>"java2s.com");
$a2=array("d"=>"Dog","b"=>"Bed","p"=>"PHP");
$a3=array("J"=>"Java","a"=>"a","d"=>"dog");
$result=array_intersect_ukey($a1,$a2,$a3,"myfunction");
print_r($result);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_key_exists() Function
- Syntax for PHP array_key_exists() Function
- Parameter for PHP array_key_exists() Function
- Return value from PHP array_key_exists() Function
- Example - Check if the key "j" exists in an array
- Example - Check if the integer key "0" exists in an array
Home » PHP Tutorial » PHP Array Functions