PHP array_intersect_ukey() Function

In this chapter you will learn:

  1. Definition for PHP array_intersect_ukey() Function
  2. Syntax for PHP array_intersect_ukey() Function
  3. Parameter for PHP array_intersect_ukey() Function
  4. Example - Intersect keys with user defined function
  5. 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

ParameterIs RequiredDescription
array1Required.Array compared with
array2Required.Array to be compared with array1
array3,...Optional.Array to be compared with array1
myfunctionRequired.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:

  1. Definition for PHP array_key_exists() Function
  2. Syntax for PHP array_key_exists() Function
  3. Parameter for PHP array_key_exists() Function
  4. Return value from PHP array_key_exists() Function
  5. Example - Check if the key "j" exists in an array
  6. Example - Check if the integer key "0" exists in an array
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function