Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示按周分组的数组中的数据_Php_Arrays_Codeigniter - Fatal编程技术网

Php 显示按周分组的数组中的数据

Php 显示按周分组的数组中的数据,php,arrays,codeigniter,Php,Arrays,Codeigniter,我已经按日期对数组进行了排序。我有数组开始索引日期和最后一个索引日期。现在我想显示我的数组,子数组按周分组。我正在使用CodeIgniter 我的控制器代码是 public function test() { if($details_sort= $this->api_model->testw()) { $msg = "OK"; $details=$details_sort; $first = reset($detai

我已经按日期对数组进行了排序。我有数组开始索引日期和最后一个索引日期。现在我想显示我的数组,子数组按周分组。我正在使用CodeIgniter

我的控制器代码是

public function test()
{
    if($details_sort= $this->api_model->testw())
    {
        $msg = "OK";

        $details=$details_sort;

        $first = reset($details);
        $last = end($details);

        $result = array($first['date'], $last['date']);

        $start_date=$first['date'];
        $last_date=$last['date'];

    }
    else
        $msg = "ERROR";

        $output = array(
            'msg' => $msg,
            'details' => $details,
            '1st' => $start_date,
            'last' => $last_date
        );

        jsonOutput($output);
    }
我当前的输出是

{
    "msg":"OK",
    "details":[
        {
            "id":"1",
            "score":"233",
            "user_id":"4",
            "date":"2014-02-03 00:00:00",

        },
        {
            "id":"2",
            "score":"1256",
            "user_id":"5",
            "date":"2014-02-05 00:00:00",

        },
        {
            "id":"4",
            "score":"123",
            "user_id":"7",
            "date":"2014-03-04 00:00:00",

        },
        {
            "id":"3",
            "score":"100",
            "user_id":"6",
            "date":"2014-03-08 00:00:00",

        },
        {
            "id":"5",
            "score":"8",
            "user_id":"2",
            "date":"2014-03-13 00:00:00",
"
        }
    ],
    "1st":"2014-02-03 00:00:00",
    "last":"2014-03-13 00:00:00"
}
但是我想把我的输出显示为

{
    "msg":"OK",
    "details":[
                "week 1":[
                    {
                        "id":"1",
                        "score":"233",
                        "user_id":"4",
                        "date":"2014-02-03 00:00:00",

                    },
                "week 2":[
                    {
                        "id":"1",
                        "score":"233",
                        "user_id":"4",
                        "date":"2014-02-03 00:00:00",

                    },

    ],
    "1st":"2014-02-03 00:00:00",
    "last":"2014-03-13 00:00:00"
}

很抱歉,我没有数据源,无法尝试此代码,但如果我的假设正确,请尝试此脚本

<?php 
public function test()
{
    $monthly = "";
    if ($details_sort = $this->api_model->testw()) {
        $msg = "OK";
        $detailsForWeek = array();
        foreach($details_sort as $dsWeek){
            $detailsForWeek[] = array('week' => $dsWeek);
        }
        $details = $detailsForWeek;
        $first = reset($details_sort);
        $last = end($details_sort);
        $result = array($first['date'], $last['date']);
        $start_date=$first['date'];
        $last_date=$last['date'];
    } else 
        $msg = "ERROR";

    $output = array(
        'msg' => $msg,
        'details' => $details,
        '1st' => $start_date,
        'last' => $last_date
    );
}

此示例使用上面提交的我当前输出的数据,并演示如何将其转换为每周数组以获取详细信息

<?php

// Take your submitted output
$jsonData = '{"msg":"OK","details":[{ "id":"1","score":"233","user_id":"4", "date":"2014-02-03 00:00:00"}, { "id":"2", "score":"1256","user_id":"5", "date":"2014-02-05 00:00:00"},{"id":"4", "score":"123", "user_id":"7", "date":"2014-03-04 00:00:00"},{ "id":"3", "score":"100", "user_id":"6", "date":"2014-03-08 00:00:00"},{ "id":"5", "score":"8", "user_id":"2", "date":"2014-03-13 00:00:00"}], "1st":"2014-02-03 00:00:00", "last":"2014-03-13 00:00:00"}';
// and convert it to an array
$origArray=json_decode($jsonData);

// We will want an array to hold data
$weeklyDetails = array();

// Lets loop through each of your details
foreach ($origArray->details as $detail){
    // and get the week number
    $weekNo=date("W", strtotime($detail->date));

    // and if we don't already have an array for that week
    if (! array_key_exists("week $weekNo", $weeklyDetails) ){
        //create it
        $weeklyDetails["week $weekNo"] = array();
    } 
    // lets push the detail into that week
    array_push($weeklyDetails["week $weekNo"], $detail);
}

// Assign the new details
$origArray->details = $weeklyDetails;

// Convert back to json
$jsonData = json_encode($origArray);

// print it out.
print($jsonData);

你的问题是什么?我的问题是如何单独显示数组数据,即按周分组。我的第一个日期和最后一个日期有更多的周,所以我想在DoXicKhow显示周数据。你计算2014-02-03 00:00:00到2014-03-13 00:00:00有多少周
{
  "msg": "OK",
  "details": {
    "week 06": [
      {
        "id": "1",
        "score": "233",
        "user_id": "4",
        "date": "2014-02-03 00:00:00"
      },
      {
        "id": "2",
        "score": "1256",
        "user_id": "5",
        "date": "2014-02-05 00:00:00"
      }
    ],
    "week 10": [
      {
        "id": "4",
        "score": "123",
        "user_id": "7",
        "date": "2014-03-04 00:00:00"
      },
      {
        "id": "3",
        "score": "100",
        "user_id": "6",
        "date": "2014-03-08 00:00:00"
      }
    ],
    "week 11": [
      {
        "id": "5",
        "score": "8",
        "user_id": "2",
        "date": "2014-03-13 00:00:00"
      }
    ]
  },
  "1st": "2014-02-03 00:00:00",
  "last": "2014-03-13 00:00:00"
}