php - 使用 cakephp 按日期分组

标签 php mysql json cakephp

我有一个表Commande,其中包含prestataire_iddateheure,...所以我想要按日期重新组合表格。我试过这个

$commande = $this->Commande->find('all', array('conditions' => array('Commande.prestataire_id' => $this->request->data['prestataire_id']),'order' =>    array('Commande.date' => 'DESC')));

json 响应是这样的

{
    "status": "Success",
    "status_code": 200,
    "message": "Commande trouvé",
    "data": {
        "get_Mission": {
            "data": [
                {
                    "Commande": {
                        "id": "475",
                        "date": "2018-04-20",
                        "heure": "10:00:00",
                        "date_created": "2018-04-19",
                        "heure_created": "17:11:39",
                        "date_accepted": "2018-04-19",
                        "heure_accepted": "17:12:35",
                        "delai": "00:00:56",
                        "date_fin": "0000-00-00",
                        "heure_fin": "00:00:00",

                    },


                },
                {
                    "Commande": {
                        "id": "476",
                        "date": "2018-04-20",
                        "heure": "10:07:00",
                        "date_created": "2018-04-19",
                        "heure_created": "17:13:12",
                        "date_accepted": "0000-00-00",
                        "heure_accepted": "00:00:00",
                        "delai": "0",
                        "date_fin": "0000-00-00",
                        "heure_fin": "00:00:00",

                    },

                },

                {
                    "Commande": {
                        "id": "477",
                        "date": "2018-04-19",
                        "heure": "17:13:00",
                        "date_created": "2018-04-19",
                        "heure_created": "17:13:48",
                        "date_accepted": "2018-04-19",
                        "heure_accepted": "17:14:23",
                        "delai": "00:00:35",
                        "date_fin": "0000-00-00",
                        "heure_fin": "00:00:00",

                    },

                }
            ]
        }
    }
}

但我希望它们像数据一样按日期重新分组 ["2018-04-20"]-> {commande,commande} ,["2018-04-19"] ->{"commande} 有什么建议吗?

最佳答案

您可以尝试将月份设置为数组键。

    foreach($examples as $example){
        $month = date('Y-m-d', strtotime($example['Model']['date']));
        $exampleArray[$month][] = $example;
    }

并在设置后重新使用您的 $exampleArray。

关于php - 使用 cakephp 按日期分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49926233/

相关文章:

php - in_array 没有任何意义

mysql - 如何关联 mysql 日志中的时间?

java - 从 mysql 数据库获取时出现空指针异常

json - WWW::Wunderground::API 无法保佑 无引用值(value)

java - @JsonProperty Json对象里面的Json对象

php - 我的第一个vps!从哪儿开始?

php - Wordpress Loop - 图像翻转

php - 在 php 中清理您的帖子标题以插入数据库以获取 SEO 链接

php - 使用 PHP 将多级 XML 加载到 MYSQL

java - 解析 json 时 GSON 中的数字格式异常