Anders Breivik
Anders Breivik

Reputation: 23

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?

Upvotes: 2

Views: 512

Answers (3)

Paul
Paul

Reputation: 141839

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

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191729

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');

Upvotes: 4

Glass Robot
Glass Robot

Reputation: 2448

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')

Upvotes: 6

Related Questions