首頁 >後端開發 >php教程 >php數組區別取值的四個函數學習

php數組區別取值的四個函數學習

WBOY
WBOY原創
2016-07-25 09:04:35888瀏覽
  1. $array1 = array("a" => "green", "red", "blue");
  2. $array2 = array("b" => "green", "yellow", "red");
  3. $result = array_intersect($array1, $array2);
  4. ?>
复制代码

上例将输出: Array ( [a] => green [0] => red )

2.array_intersect_assoc() 在前一个函数的基础上,返回所有数组中键、值均相同的键值对。

例子:

  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. ?>
复制代码

上例将输出: Array ( [a] => green )

3.array_diff() 携带多个数组,返回第一个数组中有的而后面的数组中没有的所有的值组成的新数组,对应键取自第一个数组。

例子:

  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. ?>

复制代码

上例将输出: Array ( [1] => blue )

4.array_diff_assoc() 在前一个函数的基础上,不仅需要匹配值还要匹配键。

例子:

  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. ?>
复制代码

上例将输出: Array ( => brown [c] => blue [0] => red )



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn