猿问

如何使用 PHP 获得与我需要的完全相同的 JSON 格式?

我需要这种 JSON 格式:


{

 "1" : [{"start": "", "end": ""}],

 "2" : [{"start": "", "end": ""}],

 "3" : [{"start": "", "end": ""}],

 "4" : [{"start": "", "end": ""}],

 "5" : [{"start": "", "end": ""}],

 "6" : [{"start": "", "end": ""}],

 "7" : [{"start": "", "end": ""}]

}     

但是使用 PHP 我得到了这种格式:


[

 {"1" : [{"start": "", "end": ""}]},

 {"2" : [{"start": "", "end": ""}]},

 {"3" : [{"start": "", "end": ""}]},

 {"4" : [{"start": "", "end": ""}]},

 {"5" : [{"start": "", "end": ""}]},

 {"6" : [{"start": "", "end": ""}]},

 {"7" : [{"start": "", "end": ""}]},

]

索引“1”、“2”……对应于一周中的一天。每个索引都有 'n' 个小时范围(数组),格式为 {start: "", end: ""},...


例如:{start: "12:00:00",end:"14:00:00"},{start: "14:00:00","15:00:00"},...


我使用 PHP 函数从表中获取天数,然后使用“foreach”进行迭代以在每次迭代中获取同一表中的小时范围,然后迭代这些范围以创建所需的格式。


最后一步是使用“json_encode”进行格式化。


    //Getting all days data from DB given user ID 

    $aDays = Schedule::getDays($nIdUser);


    //Array to Store Data

    $aJson = [];


    //Iteration

    foreach ($aDays as  $aInfoDay) {


        //Getting single day hour range given day ID 

        $aRangesHours = Schedule::getDayRanges("id_day = {$aInfoDay['id_day']} AND id_user = {$nIdUser}");

        $aParsedDay = [];


        //Iterate Ranges

        foreach ($aRangesHours as $aRangeHour) {

            $aParsedDay[$aRangeHour['id_day']][] = array('start' => $aRangeHour['startHour'],'end' => $aRangeHour['endHour']);

        }


        //Store Data in aJson Array

        $aJson[] = $aParsedDay;

    }


30秒到达战场
浏览 171回答 3
3回答

侃侃无极

尝试这个 :let arr = [ {"1" : [{"start": "", "end": ""}]}, {"2" : [{"start": "", "end": ""}]}, {"3" : [{"start": "", "end": ""}]}, {"4" : [{"start": "", "end": ""}]}, {"5" : [{"start": "", "end": ""}]}, {"6" : [{"start": "", "end": ""}]}, {"7" : [{"start": "", "end": ""}]},];let obj = {};arr.map((item,index) => {    obj[index + 1] = item[index + 1];});console.log(obj);

婷婷同学_

感谢您的编辑建议。在对 JSON 格式进行了一些挖掘后,我发现我需要创建数组对象而不是数组:    //Getting all days data from DB given user ID    $aDays = Schedule::getDays($nIdUser);    //Object Array to Store Data    $oJson = (object)array();    //Iteration    foreach ($aDays as  $aInfoDay) {        //Getting single day hour ranges given day ID        $aRangesHours = Schedule::getDayRanges("id_day = {$aInfoDay['id_day']} AND id_user = {$nIdUser}");        //Array to Store all Hour Ranges in a Day        $aParsedDay = array();        //Iterate Ranges        foreach ($aRangesHours as $aRangeHour) {            //Object Array to Store Single Hour Range            $oRange = (object)array();            $oRange->start = $aRangeHour['startHour'];            $oRange->end = $aRangeHour['endHour'];            //Pushing Range object to $aParsedDay Array, this is for keeping the [{"start":"","end":""},...}] format             array_push($aParsedDay,$oRange);            //Saving $aParsedDay in $oJson Object, the name of the index will be the current 'id_day'            $oJson->{$aRangeHour['id_day']} = $aParsedDay;        }    }    //result    echo json_encode($oJson);获取所需的格式:{ "1" : [{"start": "12:00:00", "end": "13:00:00"}], "2" : [{"start": "09:00:00", "end": "10:00:00"},{"start": "10:00:00", "end": "12:00:00"}], "3" : [{"start": "15:00:00", "end": "16:00:00"},{"start": "16:00:00", "end": "17:00:00"}]} 

慕森卡

当你这样做时$aJson[] = $aParseDay;,你正在考虑$aJson一个$aParseDay对象数组。您需要做的是更改要分配的第二个 foreachstart和内部end的id_day键$aJson。foreach ($aDays as  $aInfoDay) {    $aRangesHours = Schedule::getDayRanges("id_day = {$aInfoDay['id_day']} AND id_user = {$nIdUser}");    $aParsedDay = [];    foreach ($aRangesHours as $aRangeHour) {        $aJson[$aRangeHour['id_day']][] = ['start' => $aRangeHour['startHour'],'end' => $aRangeHour['endHour']];    }}
随时随地看视频慕课网APP
我要回答