Answer by Star for How do you reindex an array in PHP?
You can easily do it after use array_values() and array_filter() function together to remove empty array elements and reindex from an array in PHP. array_filter() function The PHP array_filter()...
View ArticleAnswer by Roham Rafii for How do you reindex an array in PHP?
Simply do this: <?php array_push($array, ''); $array = array_reverse($array); array_shift($array);
View ArticleAnswer by Mustapha Hadid for How do you reindex an array in PHP?
Here's my own implementation. Keys in the input array will be renumbered with incrementing keys starting from $start_index. function array_reindex($array, $start_index) { $array = array_values($array);...
View ArticleAnswer by FantomX1 for How do you reindex an array in PHP?
Similar to Nick's contribution, I came to the same solution for reindexing an array, but enhanced the function a little since from PHP version 5.4, it doesn't work because of passing variables by...
View ArticleAnswer by Mark for How do you reindex an array in PHP?
If you are not trying to reorder the array you can just do: $array = array_reverse( $array ); $array = array_reverse( $array ); The array_reverse is very fast and it reorders as it reverses. Someone...
View ArticleAnswer by Nigel Alderton for How do you reindex an array in PHP?
You can reindex an array so the new array starts with an index of 1 like this; $arr = array( '2' => 'red', '1' => 'green', '0' => 'blue', ); $arr1 = array_values($arr); // Reindex the array...
View ArticleAnswer by imagiro for How do you reindex an array in PHP?
I just found out you can also do a array_splice($ar, 0, 0); That does the re-indexing inplace, so you don't end up with a copy of the original array.
View ArticleAnswer by Nick for How do you reindex an array in PHP?
Similar to @monowerker, I needed to reindex an array using an object's key... $new = array(); $old = array( (object)array('id' => 123), (object)array('id' => 456), (object)array('id' => 789),...
View ArticleAnswer by Sam T for How do you reindex an array in PHP?
duplicate removal and reindex an array: <?php $oldArray = array('0'=>'php','1'=>'java','2'=>'','3'=>'asp','4'=>'','5'=>'mysql'); //duplicate removal $fillteredArray =...
View ArticleAnswer by Sandra for How do you reindex an array in PHP?
Here is the best way: # Array $array = array('tomato', '', 'apple', 'melon', 'cherry', '', '', 'banana'); that returns Array ( [0] => tomato [1] => [2] => apple [3] => melon [4] =>...
View ArticleAnswer by Ionuţ Pleşca for How do you reindex an array in PHP?
A more elegant solution: $list = array_combine(range(1, count($list)), array_values($list));
View ArticleAnswer by monowerker for How do you reindex an array in PHP?
Sorting is just a sort(), reindexing seems a bit silly but if it's needed this will do it. Though not in-place. Use array_walk() if you will do this in a bunch of places, just use a for-key-value loop...
View ArticleAnswer by Michael Trausch for How do you reindex an array in PHP?
You may want to consider why you want to use a 1-based array at all. Zero-based arrays (when using non-associative arrays) are pretty standard, and if you're wanting to output to a UI, most would...
View ArticleAnswer by Tom Haigh for How do you reindex an array in PHP?
$tmp = array(); foreach (array_values($array) as $key => $value) { $tmp[$key+1] = $value; } $array = $tmp;
View ArticleAnswer by Andrew Moore for How do you reindex an array in PHP?
If you want to re-index starting to zero, simply do the following: $iZero = array_values($arr); If you need it to start at one, then use the following: $iOne = array_combine(range(1, count($arr)),...
View ArticleAnswer by Peter Bailey for How do you reindex an array in PHP?
Well, I would like to think that for whatever your end goal is, you wouldn't actually need to modify the array to be 1-based as opposed to 0-based, but could instead handle it at iteration time like...
View ArticleAnswer by Greg for How do you reindex an array in PHP?
This will do what you want: <?php $array = array(2 => 'a', 1 => 'b', 0 => 'c'); array_unshift($array, false); // Add to the start of the array $array = array_values($array); // Re-number //...
View ArticleAnswer by Jason Cohen for How do you reindex an array in PHP?
If it's OK to make a new array it's this: $result = array(); foreach ( $array as $key => $val ) $result[ $key+1 ] = $val; If you need reversal in-place, you need to run backwards so you don't stomp...
View ArticleAnswer by Gumbo for How do you reindex an array in PHP?
Why reindexing? Just add 1 to the index: foreach ($array as $key => $val) { echo $key + 1, '<br>'; } Edit After the question has been clarified: You could use the array_values to reset the...
View ArticleHow do you reindex an array in PHP?
I have the following array, which I would like to reindex so the keys are reversed (ideally starting at 1): Current array (edit: the array actually looks like this): Array ( [2] => Object ( [title]...
View Article