如何在php中动态生成一些值?

时间:2016-03-16 09:09:05

标签: php arrays

我想将这些值存储在数组中

In [52]: arr = np.random.rand(200,300,300)

In [53]: w = np.random.rand(300)

In [54]: %timeit np.tensordot(arr,w,axes=([2],[0]))
100 loops, best of 3: 8.75 ms per loop

In [55]: %timeit np.einsum('ijk,k->ij',arr,w)
100 loops, best of 3: 9.78 ms per loop

In [56]: %timeit np.matmul(arr, w)
100 loops, best of 3: 9.72 ms per loop

最后一个值是$hours = array("06:00", "06:15", "06:30"....etc );

我希望它按升序存储。

我的问题是......如何自动生成这些值并将它们添加到数组中?

如果你手动添加它们......它们很多而且不好。

请告诉我一种可以动态生成这些值的方法吗?

提前致谢!

6 个答案:

答案 0 :(得分:2)

一种方法是:

data=self.tableWidget.item (0, 0)
print(data)

LIVE EXAMPLE

答案 1 :(得分:1)

使用两个for循环。外面的一个从6到23开始。内部从0到45开始增加15.加入以下:。问题解决了

答案 2 :(得分:0)

让我们狂野!

$date = \DateTime::createFromFormat('H:i', '06:00');
$stop_date = \DateTime::createFromFormat('H:i', '23:45');

$result = [];
do {
    $result[] = $date->format('H:i');
    $date->modify('+15 minutes');
} while($date <= $stop_date);

var_dump($result);

答案 3 :(得分:0)

<?php
$timeArray=array(); 
for($i=6; $i <=23; $i++)
{
    for($j=0; $j<=45; $j++)
    {
        $timeArray[]= $i.':'.$j;
        $j= $j+14;
    }
}
echo "<pre>"; print_r($timeArray);
?>

这将输出

Array
(
    [0] => 6:0
    [1] => 6:15
    [2] => 6:30
    [3] => 6:45
    [4] => 7:0
    [5] => 7:15
    [6] => 7:30
    [7] => 7:45
    [8] => 8:0
    [9] => 8:15
    [10] => 8:30
    [11] => 8:45
    [12] => 9:0
    [13] => 9:15
    [14] => 9:30
    [15] => 9:45
    [16] => 10:0
    [17] => 10:15
    [18] => 10:30
    [19] => 10:45
    [20] => 11:0
    [21] => 11:15
    [22] => 11:30
    [23] => 11:45
    [24] => 12:0
    [25] => 12:15
    [26] => 12:30
    [27] => 12:45
    [28] => 13:0
    [29] => 13:15
    [30] => 13:30
    [31] => 13:45
    [32] => 14:0
    [33] => 14:15
    [34] => 14:30
    [35] => 14:45
    [36] => 15:0
    [37] => 15:15
    [38] => 15:30
    [39] => 15:45
    [40] => 16:0
    [41] => 16:15
    [42] => 16:30
    [43] => 16:45
    [44] => 17:0
    [45] => 17:15
    [46] => 17:30
    [47] => 17:45
    [48] => 18:0
    [49] => 18:15
    [50] => 18:30
    [51] => 18:45
    [52] => 19:0
    [53] => 19:15
    [54] => 19:30
    [55] => 19:45
    [56] => 20:0
    [57] => 20:15
    [58] => 20:30
    [59] => 20:45
    [60] => 21:0
    [61] => 21:15
    [62] => 21:30
    [63] => 21:45
    [64] => 22:0
    [65] => 22:15
    [66] => 22:30
    [67] => 22:45
    [68] => 23:0
    [69] => 23:15
    [70] => 23:30
    [71] => 23:45
)

答案 4 :(得分:0)

由于您未在问题中明确说明,我假设您想要生成小时和分钟。

一种简单的方法是将值生成为自午夜起的分钟数('06:00'为60 * 6,'23:45'为23 * 60 + 45),然后将每个值格式化为“小时:分钟“使用2位数字填充0:

$list = array_map(
    // format the values as hour:minute (2-digits, left padded with 0)
    function ($item) { 
        $min = $item % 60; $hour = ($item - $min) / 60;
        return sprintf('%02d:%02d', $hour, $min);
    },
    // generate the list of values as number of minutes since midnight
    range(6 * 60, 24 * 60 - 15, 15)
);

另一种方法是使用DateTime个对象:

$start = new DateTime('06:00');
$end   = new DateTime('23:45'); 
$step  = new DateInterval('PT15M');

$list = array();
for ($date = $start; $date <= $end; $date->add($step)) {
    $list[] = $date->format('H:i');
}

请注意,如果在DST开始或结束的当天运行,此方法可能会失败。可以通过在用于初始化$start$end的字符串中确定DST未发生变化时添加一天来轻松修复:

$start = new DateTime('2016-01-01 06:00');
$end   = new DateTime('2016-01-01 23:45'); 

答案 5 :(得分:0)

此功能基于unix时间戳,但以24小时格式整齐地返回时间。

function findtime($start,$end,$interval = '15 minutes'){
   $time = array(date('H:i',$start)); //Init array and first value
   $last = $start;  


   while($last < $end){ //Loops until we reach the end value
      $last = strtotime('+' . $interval,$last); //Generates the next value
      $time[] = date('H:i',$last); //Store the new value in the desired format
   }

   return $time; 
}