刪除數組中某個值元素本實例是要利用php array_diff函數來,刪除數組中某個值元素哦,方法很簡單用foreach再加array_diff函數就
刪除數組中某個值元素
本實例是要利用php教程 array_diff函數來,刪除數組中某個值元素哦,方法很簡單用foreach再加array_diff函數就
*/
$a1 = array(array('blue','red','www.bkjia.com'),array('black','pink','green'));
$a2 = array('aaa','pink','bbbb');
$str = 'red';
$a2[] = $str;
foreach($a1 as $key => $value)
{
$a1[$key] = array_diff($value,$a2);
}
print_r($a1);
//更簡單的做法
foreach (array_diff($a1, $a2) as $_key_1) {
$arr_new[$_key_1] = $arr_1[$_key_1];
}
/*
結果如下:
array
(
[0] => array
(
[0] => blue
[2] => yellow
)
[1] => array
(
[0] => black
[2] => green
)
)
array_diff語法
array array_diff ( array $array1 , array $array2 [, array $ ... ] )
對比較array1和array2返回差異。
*/
$array1 = array("a" => "green", "php100.com", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
/*
array
(
[1] => blue
)