PHP array_diff() – Computing the Difference Between Arrays

This article explains the PHP array_diff() function, describing its purpose, parameters, return value, and provides a complete example with code that demonstrates how it returns the elements present in the first array but absent from subsequent arrays.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP array_diff() – Computing the Difference Between Arrays

The array_diff() function compares one or more arrays and returns the values from the first array that are not present in any of the other arrays.

Parameters array1 – The array to compare against the others. array2 – The array to compare with array1. ... – Optional additional arrays for comparison.

Return value

Returns an array containing all values from array1 that are not found in any of the other argument arrays, preserving the original keys.

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
)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendprogrammingPHPArrayfunctionarray-diff
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.