Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13948

Reindex an array from a specific number

How do I re-index an array starting from a specific number rather than 0.

I know it can be done by simply looping through the array and creating a new array with the Key => Value with the key being the custom number, and then just incrementing it in every iteration.

$custom_index = 5;
$output = [];
foreach($input as $val){
 $output[$custom_index++] = $val;
}

Is there any other (maybe better?) way to set the index with custom start for an array in php?

Upvotes: 2

Views: 147

Answers (3)

Nico
Nico

Reputation: 7276

Another alternative (probably not the best) can be something like this:

$array = array("five","six","seven");
$result = array_flip(array_map(function($n){
   return($n+5); // custom index
}, array_flip($array)));

print_r($result);

Output is :

Array ( [5] => five [6] => six [7] => seven ) 

We can use array_flip combined with array_map, first we switch index and values, so array_map can increment the values of the array, and then we'll switch back values and indexes.

Edit:

This is a rough comparison of performances between my code,yours and @William Janoti

$time_start = microtime(true);
$array = array_fill (0,100000,"test");
$result = array_flip(array_map(function($n){ return($n+5);}, array_flip($array)));     
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Process Time 1: {$time}";

$time_start = microtime(true);
$input =  array_fill (0,100000,"test");
$custom_index = 5;
$output = [];
foreach($input as $val){
 $output[$custom_index++] = $val;
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Process Time 2: {$time}";



$time_start = microtime(true);
$customIndex = 5;
$output = [];
// example input array
$input = array_fill (0,100000,"test");
$indexes = range($customIndex, $customIndex + count($input) - 1);
$output = array_combine($indexes, $input);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Process Time 3: {$time}";

Result :

Process Time 1: 0.012617826461792  
Process Time 2: 0.026544094085693  
Process Time 3: 0.028899908065796

Upvotes: 1

Debuqer
Debuqer

Reputation: 395

You also can use functions to implement re-index if you dont really need the actual re-indexing .

function get_array_index(&$array, $index, $index_start=0)
{
    return $array[$index_start - $index] ;
}

You probably should make new functions to use your array.

I wrote 2 functions so i can re-index the array for fake .

function array_get(&$array, $index, $index_start=0)
{
    return $array[$index - $index_start] ;
}
function array_set(&$array, $index, $val, $index_start=0)
{
    $array[$index - $index_start] = $val;
}

$index_start = 0 ;  // what is the first offset

// input 
$input = [1,2,3,4,5] ;

// get index 0
echo $input[0] ; // 1 ; this line refer is actuall array
echo '<br/>';
echo array_get($input, 0, $index_start) ; // 1 ; this is what we goona use for re-indexing purpose
echo '<br/>';

// get index 2
echo $input[2] ; // 3 
echo '<br/>';
echo array_get($input, 2, $index_start) ; // 3
echo '<br/>';

// reset $input[2] 
array_set($input, 2, 12, $index_start) ; // change 3 -> 12
echo array_get($input, 2, $index_start) ; // 12
echo '<br/>';


// let's re-index array 
$index_start = 5 ;  // first index is 5 now


// it's seems to a re-index happend but the actuall array did'nt changed

echo array_get($input, 5, $index_start) ; // must be 1
echo '<br/>';
echo array_get($input, 6, $index_start) ; // must be 2
echo '<br/>';
echo array_get($input, 7, $index_start) ; // must be 12
echo '<br/>';
echo array_get($input, 8, $index_start) ; // must be 4
echo '<br/>';
echo array_get($input, 9, $index_start) ; // must be 5
echo '<br/>';

// reset $input[9] 
array_set($input, 9, 15, $index_start) ; // change 5 -> 15
echo array_get($input, 9, $index_start) ; // 15
echo '<br/>';

If you need to re-index the array in real this functions wont help you .

Upvotes: 0

William J.
William J.

Reputation: 1584

Probably not better, but here's another way to do this:

$customIndex = 5;
$output = [];
// example input array
$input = [1,2,3,4,5];

$indexes = range($customIndex, $customIndex + count($input) - 1);
$output = array_combine($indexes, $input);

var_dump($output);

// prints out: 
array(5) {
[5]=>
int(1)
[6]=>
int(2)
[7]=>
int(3)
[8]=>
int(4)
[9]=>
int(5)
}

Upvotes: 4

Related Questions