2

For example I have array:

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

How can I remove ('a', 'b', 'c') from the array?

3 Answers 3

6

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')
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.