1

How can I remove certain items of an array?

Let's say I have an array with 10 elements, I want to remove elements at index 0, 3, and 8.

2 Answers 2

6

unset() supports removing individual array elements. The order of the remaining elements isn't affected.

unset($array[0], $array[3], $array[8]);

To reindex the array, simply call array_values() on it. Order is still maintained until you call a sorting function on it; this just reindexes:

$array = array_values($array);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, how can I reindex the array?
0

array_slice() will remove any number of consecutive elements, starting at either end.

It will also re-index your array, and has an option to not re-index.

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.