PHP array_diff() function

In this chapter you will learn:

  1. Syntax for PHP array_diff() function
  2. Description for PHP array_diff() function
  3. Parameter for PHP array_diff() function
  4. Example - Find the difference between two arrays
  5. Example - Different two associative arrays

Syntax

PHP array_diff() function has the following syntax

array array_diff ( array arr1, array arr2 [, array ...] )

Description

The array_diff() function returns a new array containing all the values of array $arr1 that do not exist in array $arr2.

You can diff several arrays by providing more parameters. The function will return an array of values in the first array that do not appear in the second and subsequent arrays.

For example:

$arr1_unique = array_diff($array1, $array2, $array3, $array4);

Parameter

ParameterIs requiredDescription
array1Required.The array to compare from
array2Required.An array to compare against
array3,...Optional.More arrays to compare against

Example 1

Find the difference between two arrays


<?PHP//from j  av a  2 s  .c o m
$toppings1 = array("A", "B", "C", "D");
$toppings2 = array("C", "D", "java2s.com");
$diff_toppings = array_diff($toppings1, $toppings2);

var_dump($diff_toppings);
?>

The code above generates the following result.

Example 2

Different two associative arrays


<?php//from   j  a  v  a2s.co m
$a1=array("a"=>"A","b"=>"B","c"=>"C","j"=>"java2s.com");
$a2=array("e"=>"E","f"=>"F","g"=>"Good");
$a3=array("a"=>"Apple","b"=>"New B","h"=>"Help");

$result=array_diff($a1,$a2,$a3);
print_r($result);
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP array_diff_assoc() Function
  2. Syntax for PHP array_diff_assoc() Function
  3. Parameter for PHP array_diff_assoc() Function
  4. Example - Compare the keys and values of two arrays, and return the differences
  5. Example - Compare the keys and values of three arrays, and return the differences
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