Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-index numeric array keys [duplicate]

I have an array that is built using the explode() function, but seeing how i'm using it with random/dynamic data, i see that the indexes keep changing:

Array
(
    [2] => Title: Warmly little before cousin sussex entire set Blessing it ladyship.
    [3] => Snippet: Testing
    [4] => Category: Member
    [5] => Tags: little, before, entire
)

I need the array to be ordered starting at 0 always. I am testing with different data and sometimes it starts at 0, and with other tests it begins at different numbers. I researched and came across Array starting at zero but it seems that only applied to that users specific case. The code i'm using to build the array can be seen here: https://stackoverflow.com/a/10484967/1183323

How can i do this?

like image 753
Tower Avatar asked May 09 '12 21:05

Tower


2 Answers

$your_new_array = array_values($your_old_array); 
like image 175
J. Bruni Avatar answered Sep 17 '22 11:09

J. Bruni


Use array_merge() to renumber the array:

$your_old_array = array( 2 => 'whatever', 19 => 'huh', 22 => 'yep' );
$your_new_array = array_merge($your_old_array);
print_r($your_new_array);

Prints this:

Array ( 
  [0] => whatever 
  [1] => huh 
  [2] => yep )
like image 24
Keith Palmer Jr. Avatar answered Sep 17 '22 11:09

Keith Palmer Jr.