News from the Field
A channel with tips on modern AI development. Join!
192 of 410 menu
◀ ▶

The array_diff Function

The array_diff function compares arrays and returns elements from the first array that are absent in the other passed arrays. Element keys are preserved. The first parameter is the main array, followed by arrays for comparison.

Syntax

array_diff(array1, array2, array3, ...);

Example

Compare two arrays and find elements from the first that are not in the second:

<?php $arr1 = [1, 2, 3, 4]; $arr2 = [2, 4, 5]; $res = array_diff($arr1, $arr2); print_r($res); ?>

Code execution result:

[0 => 1, 2 => 3]

Example

Comparison of three arrays:

<?php $arr1 = ['a', 'b', 'c', 'd']; $arr2 = ['b', 'd']; $arr3 = ['d', 'e']; $res = array_diff($arr1, $arr2, $arr3); print_r($res); ?>

Code execution result:

[0 => 'a', 2 => 'c']

Example

Comparison of associative arrays (keys are ignored, only values are compared):

<?php $arr1 = ['a' => 1, 'b' => 2, 'c' => 3]; $arr2 = ['x' => 2, 'y' => 3]; $res = array_diff($arr1, $arr2); print_r($res); ?>

Code execution result:

['a' => 1]

See Also

← →
↑
English▼
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline