php 如何更改数组键从 1 而不是 0 开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5374202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change the array key to start from 1 instead of 0
提问by Khurram Ijaz
I have values in some array I want to re index the whole array such that the the first value key should be 1 instead of zero i.e.
我在某个数组中有值我想重新索引整个数组,这样第一个值键应该是 1 而不是零,即
By default in PHP the array key starts from 0. i.e. 0 => a, 1=> b
, I want to reindex the whole array to start from key = 1 i.e 1=> a, 2=> b, ....
默认情况下,在 PHP 中,数组键从 0 开始。即0 => a, 1=> b
,我想重新索引整个数组以从键 = 1 开始,即 1=> a, 2=> b, ....
回答by Michael McTiernan
$alphabet = array("a", "b", "c");
array_unshift($alphabet, "phoney");
unset($alphabet[0]);
Edit: I decided to benchmark this solution vs. others posed in this topic. Here's the very simple code I used:
编辑:我决定将此解决方案与本主题中提出的其他解决方案进行基准测试。这是我使用的非常简单的代码:
$start = microtime(1);
for ($a = 0; $a < 1000; ++$a) {
$alphabet = array("a", "b", "c");
array_unshift($alphabet, "phoney");
unset($alphabet[0]);
}
echo (microtime(1) - $start) . "\n";
$start = microtime(1);
for ($a = 0; $a < 1000; ++$a) {
$stack = array('a', 'b', 'c');
$i= 1;
$stack2 = array();
foreach($stack as $value){
$stack2[$i] = $value;
$i++;
}
$stack = $stack2;
}
echo (microtime(1) - $start) . "\n";
$start = microtime(1);
for ($a = 0; $a < 1000; ++$a) {
$array = array('a','b','c');
$array = array_combine(
array_map(function($a){
return $a + 1;
}, array_keys($array)),
array_values($array)
);
}
echo (microtime(1) - $start) . "\n";
And the output:
和输出:
0.0018711090087891
0.0021598339080811
0.0075368881225586
回答by Ricardo Miguel
Here is my suggestion:
这是我的建议:
<?php
$alphabet = array(1 => 'a', 'b', 'c', 'd');
echo '<pre>';
print_r($alphabet);
echo '</pre>';
?>
The above example will output:
上面的例子将输出:
Array
(
[1] => a
[2] => b
[3] => c
[4] => d
)
回答by Shameer
Simply try this
试试这个
$array = array("a","b","c");
array_unshift($array,"");
unset($array[0]);
回答by kasimir
Ricardo Miguel's solution works best when you're defining your array and want the first key to be 1. But if your array is already defined or gets put together elsewhere (different function or a loop) you can alter it like this:
当您定义数组并希望第一个键为 1 时,Ricardo Miguel 的解决方案效果最佳。但是如果您的数组已经定义或放在其他地方(不同的函数或循环),您可以像这样更改它:
$array = array('a', 'b', 'c'); // defined elsewhere
$array = array_filter(array_merge(array(0), $array));
array_merge
will put an array containing 1 empty element and the other array together, re-indexes it, array_filter
will then remove the empty array elements ($array[0]
), making it start at 1.
array_merge
将包含 1 个空元素的数组和另一个数组放在一起,重新索引它,array_filter
然后删除空数组元素 ( $array[0]
),使其从 1 开始。
回答by Inigo
from the manual http://php.net/manual/en/function.array.php
来自手册 http://php.net/manual/en/function.array.php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
回答by Xin Guo
$array = array('a', 'b', 'c', 'd');
$array = array_combine(range(1, count($array)), array_values($array));
print_r($array);
the result:
结果:
Array
(
[1] => a
[2] => b
[3] => c
[4] => d
)
回答by axiom82
If you are using a range, try this code:
如果您使用的是范围,请尝试以下代码:
$data = array_slice(range(0,12), 1, null, true);
// Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 )
回答by Hos
I see this answer is very old, but my solution for this is by adding a +1 to your index. I'll do that because I think this is much faster and easy to read. When you print this it will start from 1 because 0+1 =1, then 2 etc.
我看到这个答案很旧,但我对此的解决方案是在索引中添加 +1。我会这样做,因为我认为这更快且易于阅读。当您打印它时,它将从 1 开始,因为 0+1 = 1,然后是 2 等等。
foreach($items as $index => $item){
echo $index+1 . $item
}
回答by Hardood
I think it is simple as that:
我认为这很简单:
$x = array("a","b","c");
$y =array_combine(range(1, count($x)), $x);
print_r($y);
回答by Poonam Bhatt
try this
尝试这个
<?php
$stack = array('a', 'b', 'c', 'd');
$i= 1;
foreach($stack as $value){
$stack2[$i] = $value;
$i++;
}
$stack = stack2;
?>