vote up 4 vote down star

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!

flag

4 Answers

vote up 4 vote down check
array_values($c)

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

link|flag
thx+++ works perfectly; – Kenan Sulayman Jan 31 at 19:19
vote up 2 vote down

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).

link|flag
somewhat inside... – Kenan Sulayman Jan 31 at 19:19
vote up 0 vote down

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

link|flag
Nep; I wanna remove somewhat inside.. – Kenan Sulayman Jan 31 at 19:18
vote up -1 vote down

Or reset(); is also a good choice

link|flag
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 Jan 31 at 19:18
Ok Thanks, indeed for your downvote – streetparade Jan 31 at 19:26

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.