dongxibeng5324 2017-04-11 02:59
浏览 48
已采纳

循环进入PHP中的查询和数组

I have a table containing some date for dates:

___BillableDatas:

|----------|------------|---------------|
|BIL_Id    | BIL_Date   | BIL_BookingID |
|----------|------------|---------------|
| 1        | 2017-01-01 | 10            |
| 2        | 2017-01-02 | 10            |
| 3        | 2017-01-06 | 11            |
| 4        | 2017-01-07 | 11            |
| 5        | 2017-01-08 | 11            |
| 6        | 2017-01-12 | 14            |
|----------|------------|---------------|

And I have this array giving me all the dates between two dates:

$dates = getDatesFromRange('2017-01-01', '2017-01-15');

Array ( [0] => 2017-01-01 [1] => 2017-01-02 [2] => 2017-01-03 [3] => 2017-01-04 [4] => 2017-01-05 [5] => 2017-01-06 [6] => 2017-01-07 [7] => 2017-01-08 [8] => 2017-01-09 [9] => 2017-01-10 [10] => 2017-01-11 [11] => 2017-01-12 [12] => 2017-01-13 [13] => 2017-01-14 )

I want to be able to loop into this table to get all the days between a period (from 2017-01-01 to 2017-01-15 for example).

For example, the desired output should be the following Json or array:

"datas": {
    "2017-01-01": {
        "bookingId": "10"
    },
    "2017-01-02": {
        "bookingId": "10"
    },
    "2017-01-03": {
        "bookingId": "0"
    },
    "2017-01-04": {
        "bookingId": "0"
    },
    "2017-01-05": {
        "bookingId": "0"
    },
    "2017-01-06": {
        "bookingId": "11"
    },
    "2017-01-07": {
        "bookingId": "11"
    },
    "2017-01-08": {
        "bookingId": "11"
    },
    "2017-01-09": {
        "bookingId": "0"
    },
    "2017-01-10": {
        "bookingId": "0"
    },
    "2017-01-11": {
        "bookingId": "0"
    },
    "2017-01-12": {
        "bookingId": "14"
    },
    "2017-01-13": {
        "bookingId": "0"
    },
    "2017-01-14": {
        "bookingId": "0"
    },
    "2017-01-15": {
        "bookingId": "0"
    }
}

Any help please.

Thanks.

  • 写回答

2条回答 默认 最新

  • dongyao2001 2017-04-11 05:16
    关注

    If i am not wrong you wants result including all the date range between two dates regardless it's present in DB table or not

    you can loop and compare available date and create your desire array or json.

    $new_array = array();
     foreach ($dates as $date){  // date range from "2017-01-01" to "2017-01-15"  
         $avalilable = 0;
            foreach ($bills as $bill){ // records From __BillableDatas table 
                if($bill->BIL_Date == $date)
                {
                    $avalilable = 1;
                    $new_array[$date] = array("bookingId" =>  $bill->BIL_BookingID);
                    break;  
                }   
        } 
        if(!$avalilable)
            $new_array[$date] = array("bookingId" =>  0);
     }
       echo  json_encode($new_array);
    

    Hope this will help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式