强制以2位数格式将数字推送到数组,小于10

时间:2016-09-03 10:13:57

标签: php

请您看一下这个演示,让我知道如何将01, 02, 03,..., 09中的两位数字格式(即使是小于10的数字)插入数组$days

$days = [];
for ($i = 1; $i <= 30; $i++) {
    array_push($days, $i);
} 
 echo '<pre>';
  print_r ($days);
 echo '</pre>';

输出看起来像

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 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] => 24
    [24] => 25
    [25] => 26
    [26] => 27
    [27] => 28
    [28] => 29
    [29] => 30
)

但我需要像:

Array
(
    [0] => 01
    [1] => 02
    [2] => 03
    [3] => 04
    [4] => 05
    [5] => 06
    [6] => 07
    [7] => 08
    [8] => 09
    [9] => 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] => 24
    [24] => 25
    [25] => 26
    [26] => 27
    [27] => 28
    [28] => 29
    [29] => 30
)

4 个答案:

答案 0 :(得分:1)

你的代码有点像这样

function zero_prefix($num)
{
     if($num <= 9)
     {
         return '0'.$num;
     }
     return $num;
}

$days = [];
for ($i = 1; $i <= 30; $i++) {
    $proper_format_number = zero_prefix($i);
    array_push($days, $proper_format_number );
} 
echo '<pre>';
print_r ($days);
echo '</pre>';

您想要的数组中的结果

答案 1 :(得分:0)

您可以使用sprintf将其格式化为固定宽度字符串。 %0说明符用于填充(默认填充是空格),因此我们添加另一个0作为填充字符,并指定最小宽度2 。因此sprintf("%002s", $i)会为01 - 09提供低于10的数字( s为字符串格式说明符)。

$days = [];

for ($i = 1; $i <= 30; $i++) {
    $days[] = sprintf("%002s", $i);
} 

var_dump($days);

这会给你。

array(30) {
  [0]=>
  string(2) "01"
  [1]=>
  string(2) "02"
  [2]=>
  string(2) "03"
  [3]=>
  string(2) "04"
  [4]=>
  string(2) "05"
  [5]=>
  string(2) "06"
  [6]=>
  string(2) "07"
  [7]=>
  string(2) "08"
  [8]=>
  string(2) "09"
  [9]=>
  string(2) "10"
  [10]=>
  string(2) "11"
  [11]=>
  string(2) "12"
  [12]=>
  string(2) "13"
  [13]=>
  string(2) "14"
  [14]=>
  string(2) "15"
  [15]=>
  string(2) "16"
  [16]=>
  string(2) "17"
  [17]=>
  string(2) "18"
  [18]=>
  string(2) "19"
  [19]=>
  string(2) "20"
  [20]=>
  string(2) "21"
  [21]=>
  string(2) "22"
  [22]=>
  string(2) "23"
  [23]=>
  string(2) "24"
  [24]=>
  string(2) "25"
  [25]=>
  string(2) "26"
  [26]=>
  string(2) "27"
  [27]=>
  string(2) "28"
  [28]=>
  string(2) "29"
  [29]=>
  string(2) "30"
}

答案 2 :(得分:0)

使用简单的array_mapstr_pad

<?php

 $arr =range(1,30);

  $ss = array_map(function($item) {

     return str_pad($item, 2, '0', STR_PAD_LEFT); 


  }, $arr);

  print_r($ss);

<强>输出:

   Array
   (
     [0] => 01
     [1] => 02
     [2] => 03
     ........
   )

答案 3 :(得分:0)

嘿@Suffii只是提出一个条件,如果$ i> 10然后只是将$ i连接到0,如下所示:

<?php 
$days = [];
for ($i = 1; $i <= 30; $i++) {
    if($i<10){  
        $j=(0).($i);
    array_push($days,$j);
    }else{

    array_push($days,$i);
}
} 
 echo '<pre>';
  print_r ($days);
 echo '</pre>';
?>

这是工作示例尝试一下。