Change language

PHP array_diff () function

Syntax:
array_diff ($array1, $array2, $array3, ..., $arrayn) 
Parameters : the function can take any number of arrays as parameters to be compared.Return type : this function compares the first array of parameters with the rest of the arrays and returns an array containing all the entries from $array1 that are not in any of the other arrays.Examples:
Input: $array1 = (’a’,’ b’, ’c’); $array2 = (’a’,’ d’, ’e’); $array3 = (’a’,’ b’, ’f’); array_diff ($array1, $array2, $array3); Output: Array ([2] = > c) Input: $array1 = (’a’,’ b’, ’a’); $array2 = (’a’,’ d’, ’e’); Output: Array ([1] = > b) 
The following program illustrates how array_diff() works in PHP:
// PHP code to illustrate how array_diff() works function Difference ( $array1 , $array2 , $array3 ) { return ( array_diff ( $array1 , $array2 , $array3 )); }  
// Driver code $array1 = array ( ’a’ , ’b’ , ’ c’ , ’ d’ , ’e’ , ’f’ ); $array2 = array ( ’a’ , ’b’ , ’ g’ , ’h’ ); $array3 = array ( ’a’ , ’f’ , ’ i’ ); print_r (Difference ( $array1 , $array2 , $array3 )); ?>
Output:
Array ([ 2] = > c [3] = > d [4] = > e) 
Important points to pay attention to :
  • It compares elements in their string representation. That is, 1 and 1 are equal for array_diff().
  • The number of times an element is repeated in the first array does not matter. That is, if an element occurs 3 times in $array1 and only 1 time in other arrays, then all 3 occurrences of this element in the first array will be omitted from the output.
  • For multidimensional arrays, we need to compare each of the dimensions in separately. For example: - $array1 [2], $array2 [2], etc.
Link : http://php.net/manual/en/function.array-diff.php

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically