我已经明确了数组日期。我有数组开始索引日期和最后一个索引date.now想要显示我的数组与子数组按周分组。我正在使用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"
}
答案 0 :(得分:1)
此示例使用上面提交的 我当前输出 中的数据,并演示如何将其转换为每周数组以获取详细信息
<?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);
这将提供以下输出:
{
"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"
}
答案 1 :(得分:0)
很抱歉,但我没有数据源,我无法尝试此代码,但如果我的假设是正确的,请尝试此脚本..
<?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
);
}