array_diff

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

array_diffComputes the difference of arrays

Description

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

Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.

Parameters

array

The array to compare from

arrays

Arrays to compare against

Return Values

Returns an array containing all the entries from array that are not present in any of the other arrays. Keys in the array array are preserved.

Changelog

Version Description
8.0.0 This function can now be called with only one parameter. Formerly, at least two parameters have been required.

Examples

Example #1 array_diff() example

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

print_r($result);
?>

Multiple occurrences in $array1 are all treated the same way. This will output :

Array
(
    [1] => blue
)

Example #2 array_diff() example with non-matching types

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. That is, when the string representation is the same.

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);

// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;

  public function 
__construct(string $v) {
    
$this->$v;
  }

  public function 
__toString() {
    return 
$this->v;
  }
}

$source = [new S('a'), new S('b'), new S('c')];
$filter = [new S('b'), new S('c'), new S('d')];

$result array_diff($source$filter);

// $result now contains one instance of S('a');
?>

To use an alternate comparison function, see array_udiff().

Notes

Note:

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial