1

I am trying to push values in an array and it comes like

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

But I want it to be an object like


{

"Monday" : [ "11:30", "12:00", "23:00"]

"Tuesday" : ["11:30","12:00","23:00"]

}

Here is my code snippet

$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;
4
  • Did you intentionally skip "Monday":"23:00"? Commented May 29, 2020 at 12:40
  • sorry, It was a mistake... I correct it. Commented May 29, 2020 at 12:46
  • Instead of json_encode($doctor_timings); try collect($doctor_timings)->toJson() Commented May 29, 2020 at 13:52
  • collect($doctor_timings)->toJson() is same as json_encode($doctor_timings) ... just another way to converting it. Commented May 29, 2020 at 14:29

1 Answer 1

1

You may use mapToGroups to get in the requested format. But i don't know your table structure so i got these;

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

put into the collection (assuming your eloquent collection returns like that)

$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]];
    });

which prints these;

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

Edit: (the same array you posted in online used here + same function i used for solution)

$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]];
});

which prints

{"Monday":["11:30","12:30"]}
Sign up to request clarification or add additional context in comments.

8 Comments

$doctor_timings=[]; $day_id = Monday ; $time_id = 11:30 ; array_push($doctor_timings, array($day_id => $time_id)) ; result = [ { "Monday" : "11:30" } ].... Instead of returning an array like ["Monday" => "11:30"] it gives me the above result
@Demon37 i don't get what do you mean ?
I didn't work for me. I am obtaining $day and $time from 2 different tables based on user_id and then pushing it into an empty array. There is a difference between your and mine array structure e.g. my $day_id = Monday; $time_id=11:30; array_push($doctor_timings, $day_id => $time_id); gives me this [ { " Monday " : " 11:30 " } ] thing not this [ ["Monday"=>"11:30"] ]
Can you please elaborate here sandbox.onlinephpfunctions.com/code/…
@Demon37 i edited and used your function + with my "same" solution
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.