按日期对laravel 5.1集合响应进行排序

时间:2016-08-10 06:22:00

标签: php api laravel collections response

我从API调用得到一个响应,如下所示:

[
{
date: {
date: "2016-08-03 08:17:18.000000",
timezone_type: 3,
timezone: "CET"
},
pageTitle: "Page 1",
visitors: 3,
pageViews: 27
},
{
date: {
date: "2016-08-03 08:17:18.000000",
timezone_type: 3,
timezone: "CET"
},
pageTitle: "Page 2",
visitors: 2,
pageViews: 13
},
{etc...}
]

每天返回每个页面的访问者。我想过滤,以便它返回每天所有页面的合并访问者。我想我需要先按日期(日)排序。我尝试过这样的事情没有成功:

->groupBy(function($item){
return $item->date->date->format('d-M-y')
)});

一些帮助会很棒

1 个答案:

答案 0 :(得分:1)

  

由于您的API响应是JSON,您可能需要首先将JSON数据转换为标准PHP对象,然后使用foreach构建一个新的Collection来满足您的需求....下面的注释代码显示了明确地执行步骤,您甚至可以测试此here的模拟示例:

<?php 

    // SIMULATE A JSON STRING (BASED ON YOUR DATA) TO BE CONVERTED TO PHP OBJECT
    // JUST FOR TESTING PURPOSES...
    $apiResponse = '[
        {
            "date": {
                "date": "2016-08-03 08:17:18.000000",
                "timezone_type": 3,
                "timezone": "CET"
            },
            "pageTitle": "Page 1",
            "visitors": 3,
            "pageViews": 27
        },
        {
            "date": {
                "date": "2016-08-03 08:17:18.000000",
                "timezone_type": 3,
                "timezone": "CET"
            },
            "pageTitle": "Page 2",
            "visitors": 2,
            "pageViews": 13
        }
    ]';


    // DECODE THE JSON DATA BACK INTO PHP OBJECT
    $data   = json_decode($apiResponse);

    // CREATE AN ARRAY TO HOLD THE FILTERED RESULT
    $sorted = [];

    // LOOP THROUGH THE DATA ($data) & THEN TO CHECK IF EACH ENTRY THROUGH THE ITERATION
    // HAS SIMILAR DATES (WITHOUT THE TIME PART)
    // IF IT DOES THEN ADD MERGE THE DATA TOGETHER AND PUSH IT TO THE ARRAY: $sorted...
    foreach($data as $k=>$item){
        // GET ONLY THE DATE PORTION WITHOUT THE TIME....
        $dateNoTime     = current(explode(" ", $item->date->date));
        if(!array_key_exists($dateNoTime, $sorted)){
            $sorted[$dateNoTime]                    = $item;
        }else{
            $pageViews  = $sorted[$dateNoTime]->pageViews;
            $visitors   = $sorted[$dateNoTime]->visitors;
            $pageTitle  = $sorted[$dateNoTime]->pageTitle;
            $sorted[$dateNoTime]->pageViews   = $pageViews + $item->pageViews;
            $sorted[$dateNoTime]->visitors    = $visitors  + $item->visitors;
            $sorted[$dateNoTime]->page_title  = ($pageTitle != $item->pageTitle) ?
                                                ($pageTitle . ", {$item->pageTitle}") : $pageTitle;
        }
    }

    var_dump($sorted);
    // PRODUCES::
    array (size=1)
      '2016-08-03' => 
        object(stdClass)[223]
          public 'date' => 
            object(stdClass)[221]
              public 'date' => string '2016-08-03 08:17:18.000000' (length=26)
              public 'timezone_type' => int 3
              public 'timezone' => string 'CET' (length=3)
          public 'pageTitle' => string 'Page 1' (length=6)
          public 'visitors' => int 5
          public 'pageViews' => int 40
          public 'page_title' => string 'Page 1, Page 2' (length=14)
  

在您的情况下,有效代码将

<?php 

    $data   = json_decode($apiResponse); //<== WHERE $apiResponse IS YOUR RESPONSE
    $sorted = [];
    foreach($data as $k=>$item){
        $dateNoTime     = current(explode(" ", $item->date->date));
        if(!array_key_exists($dateNoTime, $sorted)){
            $sorted[$dateNoTime]              = $item;
        }else{
            $pageViews  = $sorted[$dateNoTime]->pageViews;
            $visitors   = $sorted[$dateNoTime]->visitors;
            $pageTitle  = $sorted[$dateNoTime]->pageTitle;
            $sorted[$dateNoTime]->pageViews   = $pageViews + $item->pageViews;
            $sorted[$dateNoTime]->visitors    = $visitors  + $item->visitors;
            $sorted[$dateNoTime]->page_title  = ($pageTitle != $item->pageTitle) ?
                                                ($pageTitle . ", {$item->pageTitle}") : $pageTitle;
        }
    }

    // THE VARIABLE CONTAINING YOUR RESULT IS: $sorted  ;-)

更新:排序 - 使用KEYS“visitor”,“formattedDate”,FIRST

<?php

    $data               = json_decode($apiResponse);
    $sorted             = [];
    foreach($data as $k=>$item){
        $dateNoTime     = current(explode(" ", $item->date->date));
        if(!array_key_exists($dateNoTime, $sorted)){
            $sorted[$dateNoTime]                = new stdClass();
            $sorted[$dateNoTime]->visitors      = $item->visitors;
            $sorted[$dateNoTime]->formattedDate = $dateNoTime;
            $sorted[$dateNoTime]->pageViews     = $item->pageViews;
            $sorted[$dateNoTime]->pageTitle     = $item->pageTitle;
            $sorted[$dateNoTime]->date          = $item->date;
        }else{
            $pageViews  = $sorted[$dateNoTime]->pageViews;
            $visitors   = $sorted[$dateNoTime]->visitors;
            $pageTitle  = $sorted[$dateNoTime]->pageTitle;
            $sorted[$dateNoTime]->pageViews     = $pageViews + $item->pageViews;
            $sorted[$dateNoTime]->visitors      = $visitors  + $item->visitors;
            $sorted[$dateNoTime]->pageTitle     = ($pageTitle != $item->pageTitle) ?
                ($pageTitle . ", {$item->pageTitle}") : $pageTitle;
        }
    }
    $sorted  = array_values($sorted);

    // GET THE FIRST "OBJECT" IN THE COLLECTION:
    $objData = current($sorted);

    // GET THE NEXT (2ND) "OBJECT" IN THE COLLECTION:
    $objData = next($sorted);  //<== RETURNS false IF THERE IS NONE...

    // GET THE 3RD "OBJECT" IN THE COLLECTION:
    $objData = next($sorted);  //<== RETURNS false IF THERE IS NONE...

干杯&amp;好运!!!