Array Functions in PHP: PART 6

Introduction

In this article I describe the PHP array functions array_intersect_key, array_intersect_uassoc and array_intersect_ukey. To learn some other math functions, go to:

  1. Array Functions in PHP: PART 1
     
  2. Array Functions in PHP: PART 2
     
  3. Array Functions in PHP: PART 3
     
  4. Array Functions in PHP: PART 4
     
  5. Array Functions in PHP: PART 5

PHP array_intersect_key Function

The array_intersect_key function computes the intersection of arrays using keys for compression and returns an array with the key and values from the first array but the key is present in all of the other arrays.

Syntax

array_intersect_key (array1,array2,array3.........)

Parameters in the array_intersect_key function

Parameter Description
array1 It specifies an array, who is compared with the others.
array2 It  specifies an array to compare against the first array.
................ It specifies more arrays to compare against the first array.

Example of array_intersect_key Function

<?php
echo
"<pre>";
$
val = array("a" => "Apple", "b" => "Mango", "c" => "Orange", "Banana");
$val2 = array("
a" => "Apple", "b"=>"Raspberry", "banana");
$key = array_intersect_key($val,$val2);
print_r($key);

?>

Output

array-intersect-key-function-in-php.jpg

PHP array_intersect_uassoc Function

The array_intersect_uassoc function computes the intersection of an array with additional index checks and compares indexes using a user-defined function and returns an array with the key and values from the first array, if the function allows it.

Syntax

array_intersect_uassoc (array1,array2,array3.........UserdefinedFunction)

Parameters in the array_intersect_uassoc function

Parameter Description
array1 It specifies an array, that is compared to others.
array2 It  specifies an array to compare against the first array.
................ It specifies more arrays to be compared to the first array.
UserdefinedFunction It specifies the user-defined function

Example of array_intersect_uassoc Function

<?php

function myfunction($array1,$array2)

{

if ($array1===$array2)

{

return 0;

}

if ($array1>$array2)

{

return 1;

}

else

{

return -1;

}

}

$array1 = array(1 => "Apple",  2=> "Mango", 3 => "Orange", "Banana");

$array2 = array(1 => "Apple", 2=>"Raspberry", "banana",4 => "Orange");

echo "<pre>";

$result = array_intersect_uassoc($array1, $array2, "myfunction");

print_r($result);

?>

Output

array-intersect-uassoc-function-in-php.jpg 

PHP array_intersect_ukey Function

The array_intersect_ukey function computes the intersection of arrays using a user-defined function on the basis of key of compison and returns an array with the keys and values from the first array, if the function allows it.

Syntax

array_intersect_ukey (array1,array2,array3.........UserdefinedFunction)

Parameters in the array_intersect_ukey function

Parameter Description
array1 It specifies an array, to be compared with the others.
array2 It  specifies an array to be compared against the first array.
................ It specifies more arrays to compare against the first array.
UserdefinedFunction It specifies a user-defined function

Example of the array_intersect_ukey function

<?php

function myfunction($array1,$array2)

{

if ($array1===$array2)

{

return 0;

}

if ($array1>$array2)

{

return 1;

}

else

{

return -1;

}

}

 

$array1 = array(1 => "Apple",3 => "Mango", "c" => "Orange", "Banana");

 

$array2 = array(1 => "Apple", 2=>"Raspberry", 3=>"banana");

 

echo "<pre>";

 

$result = array_intersect_ukey($array1, $array2, "myfunction");

 

print_r($result);

 

?>

Output

array-intersect-ukey-function-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all