用零填充数组

时间:2015-07-09 03:54:59

标签: php mysql arrays

我正在运行一个mysql查询,结果数组是这样的,每个月都会更改:

Array(    
[0] => Array
    (
        [day] => 2
        [count] => 10
    )

[1] => Array
    (
        [day] => 4
        [count] => 39
    )

[2] => Array
    (
        [day] => 5
        [count] => 51
    )
    )

我想增加几天,所以我得到31天,添加的将填充0,像这样:

Array(    
[0] => Array
    (
        [day] => 1
        [count] => 0
    )

[1] => Array
    (
        [day] => 2
        [count] => 10
    )

[2] => Array
    (
        [day] => 3
        [count] => 0
    )

[3] => Array
    (
        [day] => 4
        [count] => 39
    )
    )

我想用31天填充阵列,使用已经存在的日期和计数数据......就像在第二个例子中那样......第1天和第3天就在那里...所以我添加了它们计数值0 ...按顺序... 1~31天

查询非常简单:

SELECT day(`dates`) day, count(`dates`) count FROM `calls` where month(dates) = 7

所以每个月都有不同数量的“天”,有几个月没有电话。

4 个答案:

答案 0 :(得分:3)

我建议您在数据库中创建一个日历表来保存所有日期。然后,您可以从此表中进行选择,然后将您的其他数据加入,以获得每天的总计数。 This article是开始创建日历表的好地方,this stackoverflow post包含与您的问题相似的问题和答案。

还有PHP解决方案,例如使用this stackoverflow question中提到的DateTime和DateInterval类内置的PHP迭代日期范围

答案 1 :(得分:2)

同时尝试此操作,根据您的要求修改range(1,10)

[akshay@localhost tmp]$ cat test.php 
<?php

$array=array( 
              array("day"=>2,"count"=>10),  
              array("day"=>4,"count"=>39),
              array("day"=>5,"count"=>51)
            );


function modify_array($array,$range)
{
    $tmp = array();
    array_map(function($_) use (&$tmp){ $tmp[$_] = array("day"=>$_,"count"=>0); },$range);
    $output = array_combine( array_column($array,"day"), $array ) + $tmp;
    ksort($output);
    return array_values($output);
}

// Output - modify range(1,10) as per your wish
print_r( modify_array($array, range(1,10)) );

?>

<强>输出

[akshay@localhost tmp]$ php test.php 
Array
(
    [0] => Array
        (
            [day] => 1
            [count] => 0
        )

    [1] => Array
        (
            [day] => 2
            [count] => 10
        )

    [2] => Array
        (
            [day] => 3
            [count] => 0
        )

    [3] => Array
        (
            [day] => 4
            [count] => 39
        )

    [4] => Array
        (
            [day] => 5
            [count] => 51
        )

    [5] => Array
        (
            [day] => 6
            [count] => 0
        )

    [6] => Array
        (
            [day] => 7
            [count] => 0
        )

    [7] => Array
        (
            [day] => 8
            [count] => 0
        )

    [8] => Array
        (
            [day] => 9
            [count] => 0
        )

    [9] => Array
        (
            [day] => 10
            [count] => 0
        )

)

- 编辑评论 -

对于没有array_column

的旧版PHP
function modify_array($array,$range)
{
    $tmp = array(); 
    array_map(function($_) use (&$tmp){ $tmp[$_] = array("day"=>$_,"count"=>0); },$range);
    $output = array_combine( array_map(function($e){return $e["day"];}, $array), $array ) + $tmp;
    ksort($output);
    return array_values($output);
}

答案 2 :(得分:1)

假设您执行此操作的时间超过7月,请先将目标月份设为变量并获取天数:

$month = '7';
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, 2003);

然后运行查询并迭代结果以构建按日索引的数组:

while ($row = $query->fetch_assoc()) {
    $results[$row['day']] = $row;
}

现在你可以做一个简单的for循环并填写缺失的部分:

for ($i = 1; $i <= $daysInMonth; $i++) {
    if (!isset($results[$i])) {
        $results[$i] = array(
            'day' => $i,
            'count' => 0
        );
    }
}

答案 3 :(得分:0)

302 https://api.instagram.com/oauth/authorize/?client_id=cb0096f08a3848e67355f&redirect_uri=https://www.dashboard.com/whathappened&response_type=code
200 https://instagram.com/accounts/login/?force_classic_login=&next=/oauth/authorize/%3Fclient_id%cb0096f08a3848e67355f%26redirect_uri%3Dhttps%3A//www.smashboarddashboard.com/whathappened%26response_type%3Dcode