如何使用日期元素值按yyyy-mm对JSON数据进行分组?

时间:2017-03-17 03:58:04

标签: php arrays json date grouping

我有一些JSON数据,我想在几个月内重新组合,最终将每个组放入一个带有PHP的HTML无序列表中。

目前的JSON:

[
   {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2017-03-15"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2017-03-08"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2017-02-25"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2017-02-14"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2016-12-25"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2016-01-06"
    }
]

我认为以下结构是我需要能够按月回应无序列表的结果。请随意提出建议。

{
    "2017-03": [
        {
            "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "date": "2017-03-15"
        },
        {
            "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "date": "2017-03-08"
        }
    ],
    "2017-02": [
        {
            "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "date": "2017-02-25"
        },
        {
            "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "date": "2017-02-14"
        }
    ],
    "2016-12": [
        {
            "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "date": "2016-12-25"
        }
    ],
    "2016-01": [
        {
            "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "date": "2016-01-06"
        }
    ]
}

3 个答案:

答案 0 :(得分:1)

希望这会有效..

$string='[
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2017-03-15"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2017-03-08"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2017-02-25"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2017-02-14"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2016-12-25"
    },
    {
        "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "date": "2016-01-06"
    }
]';
$array=array();
$result=array();
$array=  json_decode($string,true);
foreach($array as $value)
{
    $key=explode("-", $value["date"]);
    unset($key[2]);
    $key=implode("-", $key);
    $result[$key][]=$value;
}
echo json_encode($result,JSON_PRETTY_PRINT);

PHP Code demo

答案 1 :(得分:1)

这应该有所帮助:

<?php
$json = '[{
    "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "date": "2017-03-15"
}, {
    "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "date": "2017-03-08"
}, {
    "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "date": "2017-02-25"
}, {
    "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "date": "2017-02-14"
}, {
    "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "date": "2016-12-25"
}, {
    "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "date": "2016-01-06"
}]';

$json_decode_array = json_decode($json, true);

$result_set = [];

foreach ($json_decode_array as $key => $value) {
    $new_key = explode("-", $value["date"]);
    $result_set_key = $new_key[0] . '-' . $new_key[1];
    $result_set[$result_set_key][] = $value;
}

print_r(json_encode($result_set,JSON_PRETTY_PRINT));

答案 2 :(得分:0)

有几种方法可以做到这一点:

输入:

$json='[
    {"title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
     "date": "2017-03-15"},
    {"title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
     "date": "2017-03-08"},
    {"title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
     "date": "2017-02-25"},
    {"title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
     "date": "2017-02-14"},
    {"title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
     "date": "2016-12-25"},
    {"title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
     "date": "2016-01-06"}
]';

方法:(Demo)干净快速的foreach循环

$array=json_decode($json,true);
foreach($array as $a){
    $result[substr($a['date'],0,7)][]=$a;  // truncate the dates before assigning as keys
}
echo json_encode($result,JSON_PRETTY_PRINT);

explode()日期只是将生成的元素作为新键插入,substr()是专门为子字符串提取而设计的。{/ p>