-1

Is there a way in php to remove elements in an array and then reindex the remaining elements? For example, here is what I want to do. In an array,

$a = array("a","b","c");

I want to delete element "b", I used unset() to do that leaving the array like ("a",null,"c"). What I really want is make the array ("a","c") after deleting "b". How can I do that? Thanks!

Michael
  • 2,015
  • 7
  • 31
  • 46
  • http://stackoverflow.com/questions/5217721/how-to-remove-array-element-and-then-re-index-array – Mat Aug 15 '11 at 08:09

3 Answers3

3

unset does not create null elements in your array. The array will be one element smaller than before.

If you want to reindex the array after removing an element, use $array = array_values($array);.

Dan Grossman
  • 50,627
  • 10
  • 109
  • 97
1

do you want to do something like

 $new_array = array_filter($a)

? You can read about array filter function and take a look at the case without callback parameter (as in my example)

mkk
  • 7,495
  • 7
  • 43
  • 62
0
unset($a[1]);
$a = array_values($a);
daGrevis
  • 20,428
  • 35
  • 96
  • 136