我想更改数组的索引例如.我有一个阵列
$a = array("a","e","i","o","u");
echo $a[0]; //output a
Run Code Online (Sandbox Code Playgroud)
这意味着这个数组有索引(0,1,2,3,4)
现在我想从索引100开始我的数组而不是0
表示索引的数组(100,200,300,400,500)
如果你想以这种方式声明一个数组,你应该这样做:
$array = array(100 => 'a', 200 => 'b', 300 => 'c', 400 => 'd', 500 => 'e');
Run Code Online (Sandbox Code Playgroud)
请注意,如果以$array较短的方式添加新元素($array[] = 'f'),则指定的键将为501.
如果要将常规数组索引转换为基于数百的索引,可以执行以下操作:
$temp = array();
foreach ($array as $key => $value) {
$temp[(($key + 1) * 100)] = $value;
}
$array = $temp;
Run Code Online (Sandbox Code Playgroud)
但也许您不需要转换任何数组,而是以这种方式访问当前的数组:
$i = $hundredBasedIndex / 100 - 1;
echo $array[$i];
// or directly
echo $array[($hundredBasedIndex / 100 - 1)];
Run Code Online (Sandbox Code Playgroud)