PHP json格式

时间:2018-10-03 21:00:01

标签: php json

我正在创建一个收件箱,其中消息的每个部分都分为几个月。

我不确定实现此目标的最佳方法是什么。也许我的表格设计效率低下?还是我缺少一种方法,如果有人介意将我指向正确的方向,那将是巨大的帮助。

我的表格设置如下:

enter image description here

我想从该表中获取以下json:

[  
   {  
      "messagesCollections":[  
         {  
            "month":"Oct19",
            "messages":[  
               {  
                  "id":2,
                  "user_id":4,
                  "time":"2019-10-0317:34:00",
                  "message":"Test",
                  "header_image":"***/imboxHeader-min.png",
                  "title":"testtitle",
                  "message_part2":"test2",
                  "middle_image":"***/inboxMiddleImage-min.png",
                  "read":0,
                  "read_time":"0000-00-0000:00:00"
               }
            ]
         },
         {  
            "month":"Sep19",
            "messages":[  
               {  
                  "id":3,
                  "user_id":4,
                  "time":"2019-09-0317:34:00",
                  "message":"Test",
                  "header_image":"***/imboxHeader-min.png",
                  "title":"testtitle",
                  "message_part2":"test2",
                  "middle_image":"***/inboxMiddleImage-min.png",
                  "read":0,
                  "read_time":"0000-00-0000:00:00"
               }
            ]
         },
         {  
            "month":"Sep18",
            "messages":[  
               {  
                  "id":4,
                  "user_id":4,
                  "time":"2018-09-0317:34:00",
                  "message":"Test",
                  "header_image":"***/imboxHeader-min.png",
                  "title":"testtitle",
                  "message_part2":"test2",
                  "middle_image":"***/inboxMiddleImage-min.png",
                  "read":0,
                  "read_time":"0000-00-0000:00:00"
               }
            ]
         }
      ]
   }
]

我正在使用以下代码:

function getMessages($id) {

        $messagesArray = [];

        $connection = new db("app");
        $result = json_decode($connection->select("SELECT * FROM messages WHERE `user_id` = '$id'"), true);

        for ($x = 0; $x <= count($result) - 1; $x++) {

            $arrayName = date("M y",strtotime($result[$x]["time"]));
            $messagesArray[0][$arrayName]["messages"][$x] = $result[$x];
        }

        return $messagesArray;
    }

产生以下结果:

 [  
   {  
      "messagesCollections":[  
         {  
            "Oct 19":{  
               "messages":[  
                  {  
                     "id":1,
                     "user_id":4,
                     "time":"2019-10-03 17:34:00",
                     "message":"Test",
                     "header_image":"https:\/\/s3.eu-west-2.amazonaws.com\/clubmission\/imboxHeader-min.png",
                     "title":"test title",
                     "message_part2":"test 2",
                     "middle_image":"https:\/\/s3.eu-west-2.amazonaws.com\/clubmission\/inboxMiddleImage-min.png",
                     "read":0,
                     "read_time":"0000-00-00 00:00:00"
                  },
                  {  
                     "id":2,
                     "user_id":4,
                     "time":"2019-10-03 17:34:00",
                     "message":"Test",
                     "header_image":"https:\/\/s3.eu-west-2.amazonaws.com\/clubmission\/imboxHeader-min.png",
                     "title":"test title",
                     "message_part2":"test 2",
                     "middle_image":"https:\/\/s3.eu-west-2.amazonaws.com\/clubmission\/inboxMiddleImage-min.png",
                     "read":0,
                     "read_time":"0000-00-00 00:00:00"
                  }
               ]
            },
            "Sep 19":{  
               "messages":{  
                  "2":{  
                     "id":3,
                     "user_id":4,
                     "time":"2019-09-03 17:34:00",
                     "message":"Test",
                     "header_image":"https:\/\/s3.eu-west-2.amazonaws.com\/clubmission\/imboxHeader-min.png",
                     "title":"test title",
                     "message_part2":"test 2",
                     "middle_image":"https:\/\/s3.eu-west-2.amazonaws.com\/clubmission\/inboxMiddleImage-min.png",
                     "read":0,
                     "read_time":"0000-00-00 00:00:00"
                  }
               }
            },
            "Sep 18":{  
               "messages":{  
                  "3":{  
                     "id":4,
                     "user_id":4,
                     "time":"2018-09-03 17:34:00",
                     "message":"Test",
                     "header_image":"https:\/\/s3.eu-west-2.amazonaws.com\/clubmission\/imboxHeader-min.png",
                     "title":"test title",
                     "message_part2":"test 2",
                     "middle_image":"https:\/\/s3.eu-west-2.amazonaws.com\/clubmission\/inboxMiddleImage-min.png",
                     "read":0,
                     "read_time":"0000-00-00 00:00:00"
                  }
               }
            }
         }
      ]
   }
]

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您没有在任何地方设置“月”;因此输出是有意义的;我还没有测试过,但这可能行得通(或者至少在正确的方向上):

GLUSTER_TOP

那我应该按月确定分组吗?编辑以防止覆盖数组