views:

280

answers:

4

Hey Folks,

how can I remove an element of an array, and reorder afterwards?

<?php
   $c = array( 0=>12,1=>32 );
   unset($c[0]);
   // will return sth. like array( 1=>32 );
?>

How can I do a "rearrange" like pointing the 1 to 0 after delete (automatically)?

Thanks!

+5  A: 
array_values($c)

will return a new array with just the values, indexed linearly.

Wim
thx+++ works perfectly;
Kenan Sulayman
+2  A: 

If you are always removing the first element, then use array_shift() instead of unset().

Otherwise, you should be able to use something like $a = array_values($a).

konforce
somewhat inside...
Kenan Sulayman
A: 

If you only remove the first item of the array, you could use array_shift($c);

Harmen
Nep; I wanna remove somewhat inside..
Kenan Sulayman
A: 

Or reset(); is also a good choice

streetparade
`reset();` is not, according to PHP.net: "reset() rewinds array 's internal pointer to the first element and returns the value of the first array element."
Harmen
Ok Thanks, indeed for your downvote
streetparade