首页 >后端开发 >php教程 >怎么实现数组自动添加的问题

怎么实现数组自动添加的问题

WBOY
WBOY原创
2016-06-23 13:21:271050浏览



如何实现以半小时 每分钟为记录  如果没有记录的 自动转0
Array
(
    [2015-12-02 11:33] => 0
    [2015-12-02 11:34] => 0
    [2015-12-02 11:35] => 0
    [2015-12-02 11:36] => 1
    [2015-12-02 11:37] => 0
    [2015-12-02 11:38] => 2
    [2015-12-02 11:44] => 1
    ..........................
)

如何实现就是没有记录的 自动添加时间且值为0? 求大神解救!!


回复讨论(解决方案)

$a = Array(    '2015-12-02 11:44' => 1,    '2015-12-02 11:36' => 1,    '2015-12-02 11:38' => 2,);ksort($a);$t = intval(strtotime(key($a)) / 30 /60) * 30 * 60;for($i=0; $i<30; $i++, $t+=60) {  $k = date('Y-m-d H:i', $t);  $r[$k] = isset($a[$k]) ? $a[$k] : 0;}print_r($r);
Array(    [2015-12-02 11:30] => 0    [2015-12-02 11:31] => 0    [2015-12-02 11:32] => 0    [2015-12-02 11:33] => 0    [2015-12-02 11:34] => 0    [2015-12-02 11:35] => 0    [2015-12-02 11:36] => 1    [2015-12-02 11:37] => 0    [2015-12-02 11:38] => 2    [2015-12-02 11:39] => 0    [2015-12-02 11:40] => 0    [2015-12-02 11:41] => 0    [2015-12-02 11:42] => 0    [2015-12-02 11:43] => 0    [2015-12-02 11:44] => 1    [2015-12-02 11:45] => 0    [2015-12-02 11:46] => 0    [2015-12-02 11:47] => 0    [2015-12-02 11:48] => 0    [2015-12-02 11:49] => 0    [2015-12-02 11:50] => 0    [2015-12-02 11:51] => 0    [2015-12-02 11:52] => 0    [2015-12-02 11:53] => 0    [2015-12-02 11:54] => 0    [2015-12-02 11:55] => 0    [2015-12-02 11:56] => 0    [2015-12-02 11:57] => 0    [2015-12-02 11:58] => 0    [2015-12-02 11:59] => 0)

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn