-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,075
  • 7
  • 32
  • 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
  • 51,866
  • 10
  • 112
  • 101
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,583
  • 7
  • 46
  • 62
0
unset($a[1]);
$a = array_values($a);
daGrevis
  • 21,014
  • 37
  • 100
  • 139