php后台关于根据某个相同的日期进行分组

原创 2018年04月16日 15:42:16
接口中发现需要需要将每天(及时Unix时间不同),因为unix时间不同,需要将这个时间转成年月日这样,然后作为条件来查询这一天直播的历史数据。然后去这一天所有的数据,技术遇到的问题是,如何将这一天的数据放在一起,用sql分组不可以,因为uninx时间是不同的。所以我采用了函数循环比例进行分组。
    **没进行分组的代码**
public function merchants_dynamic()
    {
        if ($params = Request::instance()->isPost()) {
            $p = empty($params["p"]) ? 1 :$params["p"];
            $pageSize = empty($params["pagesize"]) ? 20 : $params["pagesize"];
            $merchants_id = input('merchants_id');//商家商户id
            if (!$merchants_id) error("商户店铺id不能为空");
            $list = Db::name('live_store')->alias("a")
                    ->field("a.live_store_id,d.share,d.watch_nums,d.end_time,b.title,b.apply_id,b.cover_img,b.class_id,c.business_img,c.company_name")
                    ->join("apply b",'a.room_id=b.room_id')
                    ->join("merchants c","b.merchants_id=c.gl_merchants_id")
                    ->join("live d","d.live_id=a.live_id")
                    ->where(['c.gl_merchants_id'=>$merchants_id])
                    ->limit(($p-1)*$pageSize,$pageSize)
                    ->select();
            foreach ($list as $k=>$v){
                $list[$k]['tag']=Db::name('live_class')->where(['live_class_id'=>$v['class_id']])->value('tag');
                $list[$k]['end_time'] = date("Y-m-d",$v['end_time']);
            }
            if($list){
                $data = $list;
            }else{
                $data = [];
            }
           return success($data);
         }
    }
**这里没分组的结果**
{
    "status": "ok",
    "data": [
        {
            "live_store_id": "1",
            "share": "0",
            "watch_nums": "0",
            "end_time": "2018-04-12",
            "title": "测试开发",
            "apply_id": "35",
            "cover_img": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
            "class_id": "6",
            "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.jpg",
            "company_name": "金帅集团2",
            "tag": "直播售卖"
        },
        {
            "live_store_id": "2",
            "share": "0",
            "watch_nums": "0",
            "end_time": "2018-04-13",
            "title": "测试开发",
            "apply_id": "35",
            "cover_img": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
            "class_id": "6",
            "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.jpg",
            "company_name": "金帅集团2",
            "tag": "直播售卖"
        },
        {
            "live_store_id": "1",
            "share": "0",
            "watch_nums": "0",
            "end_time": "2018-04-12",
            "title": "测试开发",
            "apply_id": "35",
            "cover_img": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
            "class_id": "6",
            "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.jpg",
            "company_name": "王氏帝国2",
            "tag": "直播售卖"
        },
        {
            "live_store_id": "2",
            "share": "0",
            "watch_nums": "0",
            "end_time": "2018-04-13",
            "title": "测试开发",
            "apply_id": "35",
            "cover_img": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
            "class_id": "6",
            "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.jpg",
            "company_name": "王氏帝国2",
            "tag": "直播售卖"
        }
    ]
}

这里按照end_time进行分组,将同一天的数据分组出来代码段

public function merchants_dynamic()
    {
        if ($params = Request::instance()->isPost()) {
            $p = empty($params["p"]) ? 1 :$params["p"];
            $pageSize = empty($params["pagesize"]) ? 20 : $params["pagesize"];
            $merchants_id = input('merchants_id');//商家商户id
            if (!$merchants_id) error("商户店铺id不能为空");
            $list = Db::name('live_store')->alias("a")
                    ->field("a.live_store_id,d.share,d.watch_nums,d.end_time,b.title,b.apply_id,b.cover_img,b.class_id,c.business_img,c.company_name")
                    ->join("apply b",'a.room_id=b.room_id')
                    ->join("merchants c","b.merchants_id=c.gl_merchants_id")
                    ->join("live d","d.live_id=a.live_id")
                    ->where(['c.gl_merchants_id'=>$merchants_id])
                    ->limit(($p-1)*$pageSize,$pageSize)
                    ->select();
            foreach ($list as $k=>$v){
                $list[$k]['tag']=Db::name('live_class')->where(['live_class_id'=>$v['class_id']])->value('tag');
                $list[$k]['end_time'] = date("Y-m-d",$v['end_time']);
            }
            $res = array();
            foreach ($list as $key=>$val){
                $res[$val['end_time']][] = $val;
            }
            $re = [];
            foreach ($res as $ke=>$va){
                $re[]['time'] = $ke;
                foreach ($re as $a=>$v){
                    $re[$a]['list'] = $va;
                }
            }
            if($list){
                $data = $re;
            }else{
                $data = [];
            }
           return success($data);
         }
    }

这里是结果

{
    "status": "ok",
    "data": [
        {
            "time": "2018-04-12",
            "list": [
                {
                    "live_store_id": "2",
                    "share": "0",
                    "watch_nums": "0",
                    "end_time": "2018-04-13",
                    "title": "测试开发",
                    "apply_id": "35",
                    "cover_img": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
                    "class_id": "6",
                    "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.jpg",
                    "company_name": "金帅集团2",
                    "tag": "直播售卖"
                },
                {
                    "live_store_id": "2",
                    "share": "0",
                    "watch_nums": "0",
                    "end_time": "2018-04-13",
                    "title": "测试开发",
                    "apply_id": "35",
                    "cover_img": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
                    "class_id": "6",
                    "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.jpg",
                    "company_name": "王氏帝国2",
                    "tag": "直播售卖"
                }
            ]
        },
        {
            "time": "2018-04-13",
            "list": [
                {
                    "live_store_id": "2",
                    "share": "0",
                    "watch_nums": "0",
                    "end_time": "2018-04-13",
                    "title": "测试开发",
                    "apply_id": "35",
                    "cover_img": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
                    "class_id": "6",
                    "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.jpg",
                    "company_name": "金帅集团2",
                    "tag": "直播售卖"
                },
                {
                    "live_store_id": "2",
                    "share": "0",
                    "watch_nums": "0",
                    "end_time": "2018-04-13",
                    "title": "测试开发",
                    "apply_id": "35",
                    "cover_img": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
                    "class_id": "6",
                    "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.jpg",
                    "company_name": "王氏帝国2",
                    "tag": "直播售卖"
                }
            ]
        }
    ]
}

END

深入Javascript 日期对象(Date)视频课程

-
  • 1970年01月01日 08:00

php 数组分组 二维数组中,将某个键值相同的合并到一个数组中

//原数组 $arr    =   array (     0 => array         (             'initial' => 'A',             'typena...
  • aiku_de_yu
  • aiku_de_yu
  • 2015-10-10 16:11:01
  • 3879

js对象数组 根据某个共同字段 分组

js对象数组 根据某个共同字段 分组
  • xtwxfvsi
  • xtwxfvsi
  • 2016-07-26 00:03:59
  • 577

JS根据数组中对象的时间依据相同的类型分组

需求:根据后台返回来的数据按同一天的日期进行展示,后台返回的数据已经按日期排序, 参考了http://blog.csdn.net/zfangls/article/details/79280083的文...
  • bluelotos893
  • bluelotos893
  • 2018-03-22 18:41:51
  • 14

php实现「浏览记录」按日期分组

现有测试数据如下,要求实现如下图中的效果: array(4) { [0] => array(6) { ["visit_id"] => int(127) ["goods_id"...
  • e62ces0iem
  • e62ces0iem
  • 2017-06-08 21:30:48
  • 521

对后台返回的json数据按月份分组并求和展示

对取到的json数据进行按月份分组求和
  • qq_27985863
  • qq_27985863
  • 2017-10-25 15:57:44
  • 366

【java】将List中的实体按照某个字段进行分组的算法

使用语言:JAVA 如何将List中存放的实体按照某个字段进行分组呢?来看看下面的例子,假如实体中有个字段叫批次号,我们将具有相同批次号的实体放在一起,那么怎么实现呢?看下面的代码: ...
  • coolcoffee168
  • coolcoffee168
  • 2016-10-18 09:09:36
  • 11025

PHP根据二维数组的某键值合并二维数组

  • u010295877
  • u010295877
  • 2016-07-20 23:03:14
  • 1145

php数组分组问题

php二维数组按某个字段分组:
  • helin916012530
  • helin916012530
  • 2016-05-25 22:40:50
  • 1581

把json对象数组属性相同的进行分组,然后取值

把json对象数组属性相同的进行分组,然后取值
  • u013516076
  • u013516076
  • 2015-12-03 12:59:15
  • 5098
收藏助手
不良信息举报
您举报文章:php后台关于根据某个相同的日期进行分组
举报原因:
原因补充:

(最多只允许输入30个字)