Array Intersect Assoc and Array Intersect Uassoc Function in PHP

Introduction

In this article I will explain the two important functions of array, Array_intersect_assoc() and Array_intersect_uassoc() in PHP.

The Array_intersect_assoc() function creates an array containing keys and values. This function computes the intersection of an array and an additional check is to compare the data with a callback function.

Syntax

Array_intersect_assoc($array1, $array2, "Array");

 

Parameter          Description
Array1 The array with master values to check.
Array2 An array to compare values against.
Array A variable list of arrays to compare.

This function returns an associative array containing all the values in array1 that are present in all of the arguments.

Example

<?php

$array1 = array

(

"a" => "Tom",

"b" => "Horry",

"c" => "grush", "joney"

);

$array2 = array

(

"a" => "Tom",

"b" => "teenu",

"grush", "joney"

);

$result_array = array_intersect_assoc($array1, $array2

echo "<pre>";print_r($result_array);

?>

Output

array intersect1.jpg

The Array_intersect_uassoc() function computes the intersection of arrays with an additional index check and compares indexes by a callback function.

Syntax

Array_intersect_uassoc($array1, $array2, "Key_compare");

 

Parameter          Description
Array1 Initial array for comparison of the array
Array2 First array compare keys against

This function returns the values of array1 whose values exist in all of the arguments.

Example

<?php

$array1 = array

(

"a" => "tom",

"b" => "horry",

"c" => "grush", "joney"

);

$array2 = array

(

"a" => "Tom",

"B" => "horry",

"teenu", "joney"

);

echo "<pre>";print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));

?>

Output

array intersect2.jpg