php删除数组中某个值元素

本实例是要利用php array_diff函数来删除数组中某个值元素,方法很简单用foreach再加array_diff函数就可以了,实例代码如下:

  1. $a1 = array(array('blue','red','www.phpfensi.com'),array('black','pink','green'));
  2. $a2 = array('aaa','pink','bbbb');
  3. $str = 'red';
  4. $a2[] = $str;
  5. foreach($a1 as $key => $value)
  6. {//开源代码phpfensi.com
  7. $a1[$key] = array_diff($value,$a2);
  8. }
  9. print_r($a1);

php更简单的做法,代码如下:

  1. foreach (array_diff($a1, $a2) as $_key_1) {
  2. $arr_new[$_key_1] = $arr_1[$_key_1];
  3. }
  4. /*
  5. 结果如下:
  6. array
  7. (
  8. [0] => array
  9. (
  10. [0] => blue
  11. [2] => yellow
  12. )
  13. [1] => array
  14. (
  15. [0] => black
  16. [2] => green
  17. )
  18. )

array_diff语法:array array_diff ( array $array1 , array $array2 [, array $ ... ] )

对比较array1和array2返回差异,实例代码如下:

  1. */
  2. $array1 = array("a" => "green", "phpfensi.com", "blue", "red");
  3. $array2 = array("b" => "green", "yellow", "red");
  4. $result = array_diff($array1, $array2);
  5. print_r($result);
  6. /*
  7. array
  8. (
  9. [1] => blue
  10. )