我不知道如何优化

时间:2013-12-12 08:46:38

标签: php

我不知道如何优化生成:

$choices = Array
(
    ["00"] => "00"
    ["01"] => "01"
    ["02"] => "02"
    ["03"] => "03"
    ["04"] => "04"
    ["05"] => "05'
    ["06"] => "06"
    ["07"] => "07"
    ["08"] => "08"
    ["09"] => "09"
    ["10"] => "10"
    ["11"] => "11"
    ["12"] => "12"
    ["13"] => "13"
    ["14"] => "14"
    ["15"] => "15"
    ["16"] => "16"
    ["17"] => "17"
    ["18"] => "18"
    ["19"] => "19"
    ["20"] => "20"
    ["21"] => "21"
    ["22"] => "22"
    ["23"] => "23"
);

4 个答案:

答案 0 :(得分:2)

使用range功能:

$choices = range(0, 22);

如果您绝对需要foreach,则可以执行strvalstring

答案 1 :(得分:1)

说实话,除非您从外部流程获得最大价值,否则您不应该尝试对其进行优化。以这种方式明确指定每个键和值比在运行中计算它们更快。

答案 2 :(得分:0)

“范围”功能不会呈现相同的数组 改为使用:

$choices = array();
for ($i = 0; $i < 23; $i++) {
    $choices[str_pad($i, 2, '0', STR_PAD_LEFT)] = str_pad($i, 2, '0', STR_PAD_LEFT);
}

答案 3 :(得分:0)

对于字符串,你可以这样做

$choices = range(0, 22);

function makestring($value){
    return (string)$value;
}


$result = array_map('makestring',$choices,array_keys($choices));