Jon Doe
Jon Doe

Reputation: 751

Remove item from PHP arrays

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.

Upvotes: 1

Views: 204

Answers (2)

BoltClock
BoltClock

Reputation: 723538

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

Upvotes: 6

Ben
Ben

Reputation: 57209

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.

Upvotes: 0

Related Questions