The Array_diff () function in PHP

Source: Internet
Author: User

Compares the key values of two arrays and returns the difference set:

<?php$a1=array ("A" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"), $a 2=array ("e" = "red", "f" = > "Green", "g" = "blue"), $result =array_diff ($a 1, $a 2);p Rint_r ($result);? >

The Array_diff () function is used to compare the key values of two (or more) arrays and return a difference set.

The function compares the key values of two (or more) arrays and returns an array of difference sets that contain all the key values in the array being compared (array1), but not in any other parameter array (array2 or array3, and so on).

Grammar

Array_diff (Array1,array2,array3 ...);

Parameter description

Array1 required. The first array to compare with the other array.

Array2 required. The array to compare with the first array.

Array3,... Optional. The other array to compare to the first array.

Returns an array of differential sets that contains all the key values in the array being compared (array1), but not in any other parameter array (array2 or array3, and so on).

Compares the key values of three arrays and returns the difference set:

<?php    $a 1=array ("a" = "red", "b" = "green", "c" = "Blue", "D" and "Yellow");    $a 2=array ("e" = "red", "f" = "black", "G" and "purple");    $a 3=array ("a" and "Red", "B" and "Black", "h" = "yellow");    $result =array_diff ($a 1, $a 2, $a 3);    Print_r ($result);? >
<pre><?phpheader ("Content-type:text/html;charset=utf-8");/* Knowledge point one: array_diff-computes the difference set of the array prototype: Array Array_diff ( Array $array 1, array $array 2 [, array $ ...]) Function: Compares the values returned in Array1 but not in the array2 and subsequent parameter arrays. Return: Returns an array that contains all the values in array1 but not in any other parameter array. Note: The returned array key name remains unchanged. Application Background Case Study: Suppose we do a mass mailing system, and the messages are kept in the array $a. If there is this time we do not want to send to a specific number of mailboxes, there is no need to remove from $ A, because the next time there is more trouble to add. Processing method: In doing an array $b, let the system automatic difference set calculation, not in $b contains several users to send messages, the others are sent.        *///Example:/* $a = range (1,9);        Create an array containing 1,2,3,4,5,6,7,8,9 $b = Array (5,6,1); Plan to exclude 1,5,6$c = Array_diff ($a, $b); The result is 2,3,4,7,8,9*///example: $a = array (' Move ', ' Beauty ', ' learn ', ' courtyard ');     $b = Array (' a ' = = ' learn ', ' beauty '); $c = Array_diff ($a, $b); Var_export ($c);  Results for the moving courtyard/* knowledge point two: array_diff_assoc-with index check computed array of difference function prototype: array ARRAY_DIFF_ASSOC (array $array 1, array $array 2 [, array $ ...] ) Function: Calculates the difference set, which requires that the key value pair is exactly the same before subtracting. Return: Returns an array that contains all the "key-value pairs" in array1 but not in any other parameter array */$a = array (' d ' = = ' move ', ' l ' = = ' Beauty ', ' x ' = ' learn ', ' y ' = = '); $b = Array (' l ' + = ' beauty ', ' a ' = ' ' move '); $c = Array_diff_assoc ($a, $b); Var_export ($c); Result: Array (' d ' = ' = ' move ', ' x ' = ' learn ', ' y ' = ' = '), '/* knowledge point three: array_diff_key-use key names to compare computed array of difference function prototype: array Array_diff_key (array $array 1, array $array 2 [, array $ ...]) Returns an entry for a different key name, based on the key name and Array2 in Array1. This function is the same as Array_diff () except that the comparison is based on the key name and not the value. */$a = array (' d ' = = ' move ', ' l ' + = ' beauty ', ' x ' = ' learn ', ' y ' = ' = ');     $b = Array (' d ' = ' learn ', ' l ' + = ' beauty '); $c = Array_diff_key ($a, $b); Var_export ($c); Result: Array (' x ' = ' learn ', ' y ' = = ', ')//* Current functions and variants of Array_diff a total of eight are as follows, the more practical there are three array_diff_assoc-with index check computed array of difference set array _diff_key-use the key name to compare the difference set of the computed array array_diff_uassoc-use the user-provided callback function to do index checking to calculate the difference set of the array array_diff_ukey-use the callback function to compare the difference between the array of keys array_diff- Computes the difference set of the array array_udiff_assoc-the difference set of the computed array with the index check, compares the data with the callback function array_udiff_uassoc-the difference set of the computed array with the index check, compares the data and the index with the callback function array_udiff- Use the callback function to compare the data to calculate the difference set of the array */?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.