doufen2769 2016-06-29 00:38
浏览 21
已采纳

数组格式和排序

It's 3am here, i'm super tired and I have no idea how to make this thing to work. The following array is returned from database and contains info like page views, date and messages for the last 7 days:

Array
(
  [0] => Array
    (
        [vws] => 9
        [mss] => 0
        [dt] => 2016-06-27
    )

  [1] => Array
    (
        [vws] => 4
        [mss] => 0
        [dt] => 2016-06-26
    )

  [2] => Array
    (
        [vws] => 1
        [mss] => 0
        [dt] => 2016-06-24
    )

  [3] => Array
    (
        [vws] => 7
        [mss] => 0
        [dt] => 2016-06-23
    )

)

This array should contain data even if there are no page views or messaged so this is what i'm looking for:

Array
(
  [0] => Array
    (
        [vws] => 0
        [mss] => 0
        [dt] => 2016-06-22
    )

  [1] => Array
    (
        [vws] => 13
        [mss] => 1
        [dt] => 2016-06-23
    )

  [2] => Array
    (
        [vws] => 7
        [mss] => 3
        [dt] => 2016-06-24
    )

  [3] => Array
    (
        [vws] => 45
        [mss] => 10
        [dt] => 2016-06-25
    )

  [4] => Array
    (
        [vws] => 0
        [mss] => 0
        [dt] => 2016-06-26
    )

  [5] => Array
    (
        [vws] => 0
        [mss] => 0
        [dt] => 2016-06-27
    )

  [6] => Array
    (
        [vws] => 5
        [mss] => 1
        [dt] => 2016-06-28
    )

)

Sorry if I was not clear enough. I will update my question if you need more info. Thank you in advance

EDIT It seems that using dt value as a key is a good idea but I still think that the same result could be achieved more easily by checking dt value with in_array.

Thanks to @ArtisiticPhoenix , this code works just fine for now:

    $new = [];
    foreach ($data as $k => $v) {
        $new[$v['dt']] = $v;
    }

    $Date = new DateTime();
    $days = [];
    for ($i = 0; $i < 7; ++$i) {
        $days[$Date->format('Y-m-d')] = [
            'vws' => 0,
            'mss' => 0,
            'dt'  => $Date->format('Y-m-d')
        ];
        $Date->modify('-1 days');
    }
    $final = array_merge($days, $new);
  • 写回答

1条回答 默认 最新

  • dongxi7722 2016-06-29 00:54
    关注

    First build an array of each day with empty values

     //current date
    $Date = new DateTime();
    
    $days = array();
     //loop 7 times, seven days in the week  ( forget if it should be <= ) 7s to high for me to count in my head right now. :)~
    for($i = 0; $i<7; ++$i ){
        $days[$Date->format('Y-m-d')] = array(
            'vws' => 0,
            'mss' => 0,
            'dt' => $Date->format('Y-m-d')  
        );
        //subtract 1 day from today's date.
        $DateTime->modify('-1 days');
    }
    

    Next, use array_merge to merge that with the db data

     $data = array_merge( $days, $dbDays );
    

    Note the keys in the prepared array is the date. You'll want to add that to the db populated data as well, this makes array_merge replace those values instead of appending the entries on ( like it would do if they where just numbered indexes ). In any case it makes correlating the data so much easier

    Alternatively you could add them in in the while loop from the db, like this

    while( false !== ( $row = $stmt->fetch() ) ){
        $days[$row['dt']] = $row;
    }
    

    But I am not sure where the data comes from ( mysql_ mysqli_ PDO )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘