开发者

How can i delete a item at index X in array? [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

How to del开发者_如何学编程ete an element from an array in php?

I have a list of things, cars for instance

$cars[0] = "audi";
$cars[1] = "saab";
$cars[2] = "volvo";
$cars[3] = "vw";

How do i delete "volvo" from the list?


$volvoIndex = array_search('volvo', $cars);
unset($cars[$volvoIndex]);


you can do with unset

unset($cars[2]);

But after that you need to iterate array with foreach


You can use the following thing to delete the x element from array

array_splice($cars,2,1)

This will delete the 2nd element in array and return the remaining array, if you want more elemetns to be deleted change 1 to number of elements that needs to be deleted.


use this for example: unset($cars[2]);

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜