如何按日期对数组数据进行分组

时间:2015-11-26 13:29:44

标签: php arrays json sorting slim

我使用端点结果如下的API:

display

如何为这样的输出排序此JSON数组:

[
    {
        placeId: 1,
        startDateIso: "2015-11-26T12:00:00+0100",
    },
    {
        placeId: 1,
        startDateIso: "2015-11-26T13:00:00+0100",
    },
    {
        placeId: 1,
        startDateIso: "2015-11-27T10:00:00+0100",
    },
    {
        placeId: 1,
        startDateIso: "2015-11-27T11:00:00+0100",
    },
]

我想按日期对数据进行排序,并为每个数据创建一个数组。

1 个答案:

答案 0 :(得分:0)

尝试以下

var BGMusicURL:NSURL = NSBundle.mainBundle().URLForResource("BGMusicURL", withExtension: "mp3")!

输出:

// Sample input
$input = array(array('startDateIso'=>'2015-11-26T12:00:00+010', 'placeId'=>1),
               array('startDateIso'=>'2015-11-26T12:00:00+010', 'placeId'=>2),
               array('startDateIso'=>'2015-11-27T12:00:00+010', 'placeId'=>3),
               array('startDateIso'=>'2015-11-27T13:00:00+010', 'placeId'=>4));

$output = array();

foreach($input as $element) {
    $date = date('d.m.Y', strtotime($element['startDateIso'])); // Truncate time

    $output[$date][] = $element; // Group using associate array, key is the date
}

$result = array_values($output); // Converts assoicate array to array

echo json_encode($result);
相关问题