Home >Backend Development >PHP Tutorial >怎么实现数组自动添加的问题

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

WBOY
WBOYOriginal
2016-06-23 13:21:271057browse



如何实现以半小时 每分钟为记录  如果没有记录的 自动转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)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php关于位运算符Next article:周一干不干活-PHP+MySQLi