Array Functions in PHP: PART 3

Introduction

In this article I explain the PHP array functions array_diff_assoc, array_diff_key and array_diff_uassoc. To learn some other math functions, go to:

  1. Array Functions in PHP: PART 1
  2. Array Functions in PHP: PART 2

PHP  array_diff_assoc Function

The array_diff_assoc function compares two or more arrays and returns an array containing all values from array1 that are not present in the other array(s).

Syntax

array_diff_assoc(array1,array2,array3...........)

Parameter in array_diff_assoc Function

The parameters for the array_diff_assoc function are:

Parameter Description
array1 It specifies an array that is compared to the others.
array2 It  specifies an array to compare against.
.................. It specifies zero or more arrays to compare against.

Example of array_diff_assoc Function

The following example creates two arrays and compares them:

<?php

$array1=array("a" => "Apple", "b" => "Mango", "c" => "Orange", "Banana");

$array2 = array("a" => "Apple", "Raspberry", "banana");

echo "<pre>";

$result = array_diff_assoc($array1, $array2);

print_r($result);

?>

Output

array-diff-assoc-function-in-php.jpg

PHP  array_diff_key Function

The array_diff_key function compares the keys of two or more arrays and returns an array of all values from array1 whose keys are not in any other of array(s).

Syntax

array_diff_key(array1,array2,array3...........)

Parameter in array_diff_key Function

The parameters for the array_diff_key function are:

Parameter Description
array1 It specifies an array that is compared to the others.
array2 It  specifies an array to compare against.
.................. It specifies zero or more arrays to compare against.

Example of array_diff_key Function

The following example creates two arrays and compares their keys:

<?php

$array1=array("a" => "Apple", "b" => "Mango", "c" => "Orange", "Banana");

$array2 = array("a" => "Apple", "b"=>"Raspberry", "banana");

echo "<pre>";

$result = array_diff_key($array1, $array2);

print_r($result);

?>

Output

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

PHP  array_diff_uassoc Function

The array_diff_uassoc function checks the differences of array keys using a user defined function and returns an array containing all the entries from array1 that are not present in any of the other arrays.

Syntax

array_diff_uassoc(array1,array2,array3...........userDefined function)

Parameter in array_diff_uassoc Function

The parameters for the array_diff_uassoc function are:

Parameter Description
array1 It specifies an array that is compared to the others.
array2 It  specifies an array to compare against.
.................. It specifies zero or more arrays to compare against.
userDefined function It specifies the name of the user defined function.

Example of array_diff_uassoc Function

The following example creates two arrays and compares the keys using the user defined function "myfunction":

<?php

functionmyfunction($array1,$array2)

{

if ($array1===$array2)

  {

  return 0;

  }

if ($array1>$array2)

  {

  return 1;

  }

else

  {

  return -1;

  }

}

$array1 = array("a" => "Apple", "b" => "Mango", "c" => "Orange", "Banana");

$array2 = array("a" => "Apple", "b"=>"Raspberry", "banana");

echo "<pre>";

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

print_r($result);

?>

Output

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


Similar Articles