PHP get array intersection and difference set method, PHP get array intersection _php tutorial

Source: Internet
Author: User

PHP obtains the array intersection and the difference set method, PHP obtains the array intersection


This article describes how PHP obtains array intersection and difference sets. Share to everyone for your reference. The specific analysis is as follows:

First, the intersection of the array array_intersect ()

The Array_intersect () function returns an array that retains the key, which consists only of the values that appear in the first array and that appear in each of the other input arrays. The form is as follows:

Array array_intersect (array Array1,array array2[,arrayn ...])

The following example returns all the fruits that appear in the $fruit1 array and also appear in $fruit2 and $fruit3:

<?php$fruit1 = Array ("Apple", "Banana", "Orange"), $fruit 2 = Array ("Pear", "Apple", "Grape"), $fruit 3 = Array (" Watermelon "," Orange "," Apple "), $intersection = Array_intersect ($fruit 1, $fruit 2, $fruit 3);p Rint_r ($intersection);// output//Array ([0] = Apple)?>

The Array_intersect () function considers them to be the same only if the two elements are equal and have the same data type.

Intersection of associative arrays ARRAY_INTERSECT_ASSOC ()

The function Array_intersect_assoc () is basically the same as Array_intersect () except that he also considers the keys of the array in the comparison. Therefore, only key/value pairs that appear in the first array and in all other input arrays are returned to the result array.

The form is as follows:

Array Array_intersect_assoc (array Array1,array array2[,arrayn ...])

The following example returns all the key/value pairs that appear in the $fruit1 array and also in $fruit2 and $fruit3:

<?php$fruit1 = Array ("Red" = "Apple", "yellow" = "Banana", "orange" = "orange"), $fruit 2 = Array ("Yellow" = > "Pear", "Red" and "Apple", "Purple" and "Grape"); $fruit 3 = Array ("green" = "watermelon", "orange" + "orange" "Red" = "Apple"), $intersection = Array_intersect_assoc ($fruit 1, $fruit 2, $fruit 3);p Rint_r ($intersection);// output//Array ([red] = Apple)?>

Second, the difference set of the array Array_diff ()

The function Array_diff () returns a value that appears in the first array but not in the other input array. This function is opposite to Array_intersect ().

Array Array_diff (array Array1,array array2[,arrayn ...])

Examples are as follows:

<?php$fruit1 = Array ("Apple", "Banana", "Orange"), $fruit 2 = Array ("Pear", "Apple", "Grape"), $fruit 3 = Array (" Watermelon "," Orange "," Apple "), $intersection = Array_diff ($fruit 1, $fruit 2, $fruit 3);p Rint_r ($intersection);// output//Array ([1] = Banana)?>

The difference set of associative arrays Array_diff_assoc ()

The function Array_diff_assoc () is basically the same as Array_diff (), except that it also considers the keys of the array when compared. Therefore, a key/value pair that appears only in the first array and no longer appears in the other input array is returned to the result array. The form is as follows:

Array Array_diff_assoc (array Array1,array array2[,arrayn ...])

The following example returns only [yellow] + Banana, because this special key/value pair appears in $fruit1 and does not exist in $fruit2 and $fruit3.

<?php$fruit1 = Array ("Red" = "Apple", "yellow" = "Banana", "orange" = "orange"), $fruit 2 = Array ("Yellow" = > "Pear", "Red" and "Apple", "Purple" and "Grape"); $fruit 3 = Array ("green" = "watermelon", "orange" + "orange" "Red" = "Apple"), $intersection = Array_diff_assoc ($fruit 1, $fruit 2, $fruit 3);p Rint_r ($intersection);//output// Array ([yellow] = Banana)?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1014424.html www.bkjia.com true http://www.bkjia.com/PHPjc/1014424.html techarticle PHP obtains the array intersection and the difference set method, PHP obtains the array intersection this article narrates the method which PHP obtains the array intersection and the difference set. Share to everyone for your reference. The specific analysis is as follows: ...

  • Related Article

    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.