One day learning a new PHP function (2) array

Source: Internet
Author: User
Tags array arrays comparison contains expression key string

Array_diff (array array1, array array2 [, array ...])

Description
Array_diff () returns an array that includes all values that are in array1 but not in any other parameter array. Note that the key name remains unchanged.

Liezi


  
    "green", "red", "blue", "red");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_diff($array1, $array2);
    print_r($result);
?>

Output results are

Array([1] => blue)

Attention

two units are considered identical only in (string) $elem 1 = = (string) $elem 2 o'clock. In other words, when the string expression is the same. Note This function examines only one dimension of a multidimensional array. Certainly can use Array_diff ($array 1[0], $array 2[0]); Check for deeper dimensions.

Array Array_diff_assoc (array array1, array array2 [, array ...])

Description
ARRAY_DIFF_ASSOC () returns an array that includes all values that are in array1 but not in any other parameter array. Note that unlike Array_diff (), the key name is also used for comparison.
the


  
    "green", "b" => "brown", "c" => "blue", "red");
    $array2 = array("a" => "green", "yellow", "red");
    $result = array_diff_assoc($array1, $array2);
    print_r($result);
?> 

Output

Array(
    [b] => brown
    [c] => blue
    [0] => red
)

The above example can see that the key value pair "a" => "green" is in two arrays, so it is not in the output of this function. In contrast, a key value of 0 => "Red" appears in the output because the key name of "Red" in the second argument is 1.

2


  
    

Output

Array(
    [0] => 0
    [1] => 1
)    

A key value of two values in key => value is considered equal only when (string) elem1=== (String) elem2. In other words, a strict check is used, and the string must be expressed in the same way.

Attention

Note: Notice that this function examines only one dimension of a multidimensional array. Certainly can use ARRAY_DIFF_ASSOC ($array 1[0], $array 2[0]); Check for deeper dimensions.

Array Array_diff_key (array $array 1, array $array 2 [, array $ ...])

Description
Returns the entries for different key names, based on the comparison of the key names and array2 in Array1. This function is the same as Array_diff () except that the comparison is based on the key name instead of the value.
Returns an array that contains the values of all the key names that appear in Array1 but do not appear in any other parameter array.

Liezi


  
    1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
    $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

    var_dump(array_diff_key($array1, $array2));
?> 

Output

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

The two key names in key => value pairs are considered equal only in (string) $key 1 = = (string) $key 2 o'clock. In other words, strict type checking is performed, so the string expression must be exactly the same.

Array Array_intersect (array $array 1, array $array 2 [, array $ ...])

Description
Array_intersect () returns an array that contains all the values in Array1 that also appear in all other parameter arrays. Note that the key name remains unchanged.



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.