Quantcast
Channel: How do you reindex an array in PHP but with indexes starting from 1? - Stack Overflow
Browsing index pages (44 articles)

How do you reindex an array in PHP but with indexes starting from 1?

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


Answer by Gumbo for How do you reindex an array in PHP but with indexes...

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 Article


Answer by Jason Cohen for How do you reindex an array in PHP but with indexes...

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

View Article

Answer by Greg for How do you reindex an array in PHP but with indexes...

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 Article

Answer by Peter Bailey for How do you reindex an array in PHP but with...

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 Article


Answer by Andrew Moore for How do you reindex an array in PHP but with...

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 Article

Answer by Tom Haigh for How do you reindex an array in PHP but with indexes...

$tmp = array();foreach (array_values($array) as $key => $value) { $tmp[$key+1] = $value;}$array = $tmp;

View Article

Answer by Michael Trausch for How do you reindex an array in PHP but with...

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 Article


Answer by monowerker for How do you reindex an array in PHP but with indexes...

Sorting is just a sort(), reindexing seems a bit silly but if it is 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 Article


Answer by Ionuţ Pleşca for How do you reindex an array in PHP but with...

A more elegant solution:$list = array_combine(range(1, count($list)), array_values($list));

View Article

Answer by Sandra for How do you reindex an array in PHP but with indexes...

Here is the best way:# Array$array = array('tomato', '', 'apple', 'melon', 'cherry', '', '', 'banana');that returnsArray( [0] => tomato [1] => [2] => apple [3] => melon [4] => cherry [5]...

View Article

Answer by Sam T for How do you reindex an array in PHP but with indexes...

Duplicate removal and reindex an array:<?php $oldArray = array('0'=>'php','1'=>'java','2'=>'','3'=>'asp','4'=>'','5'=>'mysql'); //duplicate removal $fillteredArray =...

View Article

Answer by Nick for How do you reindex an array in PHP but with indexes...

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' =>...

View Article


Answer by imagiro for How do you reindex an array in PHP but with indexes...

I just found out you can also do aarray_splice($ar, 0, 0);That does the re-indexing inplace, so you don't end up with a copy of the original array.

View Article

Answer by Nigel Alderton for How do you reindex an array in PHP but with...

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

View Article


Answer by Mark for How do you reindex an array in PHP but with indexes...

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

View Article

Answer by FantomX1 for How do you reindex an array in PHP but with indexes...

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 Article


Answer by Mustapha Hadid for How do you reindex an array in PHP but with...

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 Article

Answer by Roham Rafii for How do you reindex an array in PHP but with indexes...

Simply do this:<?phparray_push($array, '');$array = array_reverse($array);array_shift($array);

View Article

Answer by Star for How do you reindex an array in PHP but with indexes...

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() functionThe PHP array_filter() function...

View Article
Browsing index pages (44 articles)


Latest Images