Home > Backend Development > PHP Tutorial > Learn four functions for distinguishing values ​​in php arrays

Learn four functions for distinguishing values ​​in php arrays

WBOY
Release: 2016-07-25 09:04:35
Original
874 people have browsed it
  1. $array1 = array("a" => "green", "red", "blue");
  2. $array2 = array("b" => "green", "yellow", "red");
  3. $result = array_intersect($array1, $array2);
  4. ?>
Copy the code

The above example will output: Array ( [a] => green [0] => red )

2.array_intersect_assoc() Based on the previous function, return key-value pairs with the same key and value in all arrays.

Example:

  1. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red") ;
  2. $array2 = array("a" => "green", "yellow", "red");
  3. $result_array = array_intersect_assoc($array1, $array2);
  4. ?>
Copy code

The above example will output: Array ( [a] => green )

3.array_diff() Carries multiple arrays and returns a new array composed of all the values ​​that are in the first array but not in the following arrays. The corresponding keys are taken from the first array.

Example:

  1. $array1 = array("a" => "green", "red", "blue", "red");

  2. $array2 = array( "b" => "green", "yellow", "red");
  3. $result = array_diff($array1, $array2);

  4. print_r($result);

  5. ? >

Copy the code

The above example will output: Array ( [1] => blue )

4.array_diff_assoc() Based on the previous function, not only the values ​​but also the keys need to be matched.

Example:

  1. $array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red") ;
  2. $array2 = array ("a" => "green", "yellow", "red");
  3. $result = array_diff_assoc($array1, $array2);
  4. ?>
Copy code

The above example will output: Array ( => brown [c] => blue [0] => red )



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template