4

I want to change the indexing of array for example. I have an array

$a = array("a","e","i","o","u");
echo $a[0]; //output a

It means this array has index (0,1,2,3,4)

Now I want to start my array from index 100 instead 0

Means array with index (100,200,300,400,500)

3
  • 1
    If this array is getting created dynamically,then you can easily assign the index you want in a loop. Commented Nov 22, 2012 at 9:57
  • Is there any system to this? Is is always a limited number of values? Is it dynamic? Can't you just type it by hand? Commented Nov 22, 2012 at 9:58
  • 3
    Is there an actual valid reason why you'd want this kind of behaviour? Commented Nov 22, 2012 at 10:01

7 Answers 7

5

If you want to declare an array that way you should do:

$array = array(100 => 'a', 200 => 'b', 300 => 'c', 400 => 'd', 500 => 'e');

Note that if you add a new element to the $array in the shorter way ($array[] = 'f') the key assigned will be 501.

If you want to convert regular array indexes to hundreds-based ones you can do this:

$temp = array();
foreach ($array as $key => $value) {
    $temp[(($key + 1) * 100)] = $value;
}
$array = $temp;

But perhaps you don't really need to convert no array and instead access your current one this way:

$i = $hundredBasedIndex / 100 - 1;
echo $array[$i];
// or directly
echo $array[($hundredBasedIndex / 100 - 1)];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jackflash for your answer.This is the first time when I asked question on internet. You really gave your answers in very right manner and explaining all the things. Thanks!!! :)
4

Other solution:

$a = array_combine(array(100, 200, 300, 400, 500), $a);

Comments

1

You could simply define a starting index and iterate over the array swapping the indexes. Something like this -

$oldArray = array("a","e","i","o","u");  
$newArray = array();
$startIndex = 100;

foreach($oldArray AS $key => $value){
  $newArray[$startIndex] = $value;
  startIndex += 100;
}

2 Comments

except that startIndex has to be incremented by 100.
@ise - oops! Thanks for that! How embarrassing ;-) I'm still confused about the logic behind this behavior... I hope the OP has a justified reason to do this...
0

just make an associative array with keys as indexes you want. for example :

$myArray = array(100=>"a",200=>"e",300=>"i",400=>"o",500=>"u");

then you call the values like:

echo $myArray[100] ;

note: do not put '100'=>"a" instead of 100=>"a"

Comments

0

$key = array(100, 200, 300, 400, 500);

$b = array_combine($key, $a);

Comments

-1

Simply change your array Like this:

$a = array(100 => "a", 200 => "e", 300 => "i", 400 => "o", 500 => "u");

Comments

-1

Just create the array that way.

$myarray = array(
    100 => 'a',
    200 => 'b',
);

If you already have an array with that order, and want to multiply the keys by 100, you can do so in a loop (there are other ways, but...):

$a = array('a', 'b', 'c', 'd', 'e');

$newarray = array();
foreach($a as $key => $value)
    $newarray[100*($key+1)] = $value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.