第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將數(shù)組轉(zhuǎn)換為json對(duì)象?

如何將數(shù)組轉(zhuǎn)換為json對(duì)象?

PHP
慕俠2389804 2023-04-21 17:22:53
我正在嘗試將值推送到數(shù)組中,結(jié)果就像[{"Monday":"11:30"},{"Monday":"12:00"},{"Monday":"23:00"},{"Tuesday":"11:30"},{"Tuesday":"12:00"},{"Tuesday":"23:00"}]但我希望它成為一個(gè)對(duì)象{"Monday" : [ "11:30", "12:00", "23:00"]"Tuesday" : ["11:30","12:00","23:00"]}這是我的代碼片段$timings = DoctorTimings::where('doctor_id', '=', $input['doctor_id'])->get();$doctor_timings = [];foreach($timings as $timing) {    $day_id = $timing->day_id;    $time_slot_id = $timing->time_slot_id;    $day_id =  Days::where('id', '=', $day_id)->first()->day;    $time_id = DB::table('time_slot')->where('id', '=', $time_slot_id)->select('time')->first()->time;      array_push($doctor_timings, array($day_id => $time_id));}$doctorTimings = json_encode($doctor_timings);echo $doctorTimings;
查看完整描述

1 回答

?
侃侃無(wú)極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以使用mapToGroups獲取請(qǐng)求的格式。但是我不知道你的表結(jié)構(gòu)所以我得到了這些;


[{"Monday":"11:30"},{"Monday":"12:00"},{"Monday":"23:00"},{"Tuesday":"11:30"},{"Tuesday":"12:00"},{"Tuesday":"23:00"}]

放入集合中(假設(shè)你雄辯的集合像那樣返回)


$timings = collect([

        ["Monday" => "11:30"],

        ["Monday" => "12:00"],

        ["Monday" => "23:00"],

        ["Tuesday" => "11:30"],

        ["Tuesday" => "12:00"],

        ["Tuesday" => "23:00"]

    ]);


    return $timings->mapToGroups(function ($item) {

        $day = array_key_first($item);


        return [$day => $item[$day]];

    });

打印這些;


{

  "Monday": [

    "11:30",

    "12:00",

    "23:00"

  ],

  "Tuesday": [

    "11:30",

    "12:00",

    "23:00"

  ]

}

編輯:(你在網(wǎng)上發(fā)布的相同數(shù)組在這里使用+我用于解決方案的相同功能)


$days = ["Monday", "Monday"];

$time = ["11:30", "12:30"];

$doctor_timings = [];

for ($i = 0; $i < sizeof($days); $i++) {

    $day_id = $days[$i];

    $time_id = $time[$i];

    array_push($doctor_timings, [$day_id => $time_id]);

}


return collect($doctor_timings)->mapToGroups(function ($item) {

    $day = array_key_first($item);


    return [$day => $item[$day]];

});

哪個(gè)打印


{"Monday":["11:30","12:30"]}


查看完整回答
反對(duì) 回復(fù) 2023-04-21
  • 1 回答
  • 0 關(guān)注
  • 140 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)