开发者

Remove item from PHP arrays

开发者 https://www.devze.com 2023-01-23 22:30 出处:网络
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 ar

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消