PHP array_diff function

Last Updated: February 11, 2022

array_diff : Computes the difference of arrays

Syntax

 array_diff(array $array, array ...$arrays): array

Supports (PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

Parameter

Parameter Description
arrays Required. array to compare
arrays Required. arrays to be compare against

Example


<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Output

Array
(
    [1] => blue
)

Related

array_diff_assoc – Compute the difference of arrays by comparing both keys and values

Test