John Smith
John Smith

Reputation: 489

re-index php keys in array starting with certain number

I have an array which has keys starting from 0 upwards. Is there a way to reindex the array to change shift the keys to start from 3 instead of 0?

Upvotes: 0

Views: 57

Answers (2)

Alireza Fallah
Alireza Fallah

Reputation: 4607

$newArray = array();
foreach($myArray as $key=>$value){
$newArray[$key+3] = $value;
}
print_r($newArray);

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212522

Quickly off the top of my head, but there's probably better ways to do it

$reindexedArray = array_combine(
    range(3,count($originalArray)+3),
    $originalArray
);

Upvotes: 1

Related Questions