Remove item from PHP arrays
How can I remove certain items of an array?
Let's 开发者_StackOverflow社区say I have an array with 10 elements, I want to remove elements at index 0, 3, and 8.
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);
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.
精彩评论