21

Possible Duplicate:
How to delete 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?

Community
  • 1
  • 1
Jason94
  • 12,922
  • 36
  • 102
  • 179

4 Answers4

14
$volvoIndex = array_search('volvo', $cars);
unset($cars[$volvoIndex]);
deceze
  • 491,798
  • 79
  • 706
  • 853
4

you can do with unset

unset($cars[2]);

But after that you need to iterate array with foreach

Shakti Singh
  • 81,083
  • 20
  • 131
  • 150
3

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.

user598006
  • 31
  • 1
0

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

Tom Granot
  • 1,778
  • 4
  • 21
  • 51