开发者

Remove elements from array

For example I have array:

$arr = array('a', 'b', 'c', 'd', 'e', 'f');

How can I remove (开发者_开发知识库'a', 'b', 'c') from the array?


Unset will remove them:

unset($arr[0], $arr[1], $arr[2]);

And there is array_slice:

array_slice($arr, 3); 

Returns:

array('d', 'e', 'f')


There are several ways to do this. The optimal really depends on your input.

If you have an array of the values you need to remove, which is probably your case, this will work best:

$arr = array('a', 'b', 'c', 'd', 'e', 'f');
$bad = array('a', 'b', 'c');

$good = array_diff($arr, $bad); //returns array('d', 'e', 'f');


$arr = array('a', 'b', 'c', 'd', 'e', 'f');
$remove = array('a', 'b', 'c');
$arr = array_diff($arr, $remove);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜