0

I have an array of hospital which doctors are visiting at different time of weekdays eg:

Array
(
  [0] => stdClass Object
      (
        [id] => 40
        [doctorid] => 58
        [hospitalid] => 1
        [weekday] => 2
        [starttime] => 09:30
        [stime] => PM
        [endtime] => 11:00
        [etime] => PM
        [randomkey] => 
      )

  [1] => stdClass Object
      (
        [id] => 41
        [doctorid] => 58
        [hospitalid] => 1
        [weekday] => 2
        [starttime] => 02:01
        [stime] => PM
        [endtime] => 04:02
        [etime] => PM
        [randomkey] => 
      )

 [2] => stdClass Object
      (
        [id] => 39
        [doctorid] => 58
        [hospitalid] => 1
        [weekday] => 3
        [starttime] => 09:30
        [stime] => AM
        [endtime] => 11:00
        [etime] => AM
        [randomkey] => 
      )

  [3] => stdClass Object
      (
        [id] => 38
        [doctorid] => 58
        [hospitalid] => 1
        [weekday] => 3
        [starttime] => 06:30
        [stime] => PM
        [endtime] => 09:30
        [etime] => PM
        [randomkey] => 
      )

  [4] => stdClass Object
      (
        [id] => 7
        [doctorid] => 58
        [hospitalid] => 2
        [weekday] => 2
        [starttime] => 09:30
        [stime] => AM
        [endtime] => 05:00
        [etime] => PM
        [randomkey] => rKkEU6cTWWN4ZxCw
      )

  [5] => stdClass Object
      (
        [id] => 8
        [doctorid] => 58
        [hospitalid] => 3
        [weekday] => 2
        [starttime] => 09:30
        [stime] => AM
        [endtime] => 05:00
        [etime] => PM
        [randomkey] => rKkEU6cTWWN4ZxCw
      )

  [6] => stdClass Object
      (
        [id] => 9
        [doctorid] => 58
        [hospitalid] => 23
        [weekday] => 2
        [starttime] => 09:30
        [stime] => AM
        [endtime] => 06:00
        [etime] => PM
        [randomkey] => rKkEU6cTWWN4ZxCw
      )

    )

I want the array in the following JSON tree format first order by hospital, days then time Eg:

    {
"Hospital": [
    {
        "doctorid": "58",
        "hospitalid": "1",
        "day": [
            {
                "weekday": 2,
                "time": [
                    {
                        "starttime": "9:30",
                        "stime": "AM",
                        "endtime": "11:00",
                        "etime": "PM"
                    },
                    {
                        "starttime": "2:01",
                        "stime": "PM",
                        "endtime": "04:02",
                        "etime": "PM"
                    }
                ]
            },
            {
                "weekday": 3,
                "time": [
                    {
                        "starttime": "9:30",
                        "stime": "AM",
                        "endtime": "11:00",
                        "etime": "PM"
                    },
                    {
                        "starttime": "6:30",
                        "stime": "PM",
                        "endtime": "09:30",
                        "etime": "PM"
                    }
                ]
            }
        ]
    },
    {
        "doctorid": "58",
        "hospitalid": "2",
        "day": [
            {
                "weekday": 2,
                "time": [
                    {
                        "starttime": "9:30",
                        "stime": "AM",
                        "endtime": "05:00",
                        "etime": "PM"
                    }
                ]
            }
        ]
    },
    {
        "doctorid": "58",
        "hospitalid": "3",
        "day": [
            {
                "weekday": 2,
                "time": [
                    {
                        "starttime": "9:30",
                        "stime": "AM",
                        "endtime": "05:00",
                        "etime": "PM"
                    }
                ]
            }
        ]
    },
    {
        "doctorid": "58",
        "hospitalid": "23",
        "day": [
            {
                "weekday": 3,
                "time": [
                    {
                        "starttime": "9:30",
                        "stime": "AM",
                        "endtime": "06:00",
                        "etime": "PM"
                    }
                ]
            }
        ]
    }
]
}

I have tried to breakdown using foreach loop but can't solve it after hospital level. A generic functions will be helpful.

1
  • 3
    please post attempted code Commented Mar 1, 2015 at 20:57

1 Answer 1

1

You need to go through your original array and create the final array, sorted as you want. Then calling json_encode($finalArray, true) will give you the json representation.

In your example, you can do something like this:

$sortedHospitals = [];
foreach ($hospitals as $hospital) {
    $hopitalId = $hospital['id'];
    $weekday = $hospital['weekday'];
    $startTime = $hospital['startTime'];
    $endTime = $hospital['endTime'];

    if (array_key_exists($hospitalId, $sortedHospitals)) {
        $currentHospital = $sortedHospitals[$hospitalId];
        // add time to current hospital
    }
    else {
        // create correct structure for the hospital
        $currentHospital = [
            'id' => $hospitalId,
            'doctorId' => $hospital['doctorid'],
            ...
        ];
        $sortedHospitals[$hospitalId] = $currentHospital;
    }
}

return json_encode($sortedHospitals, true);
Sign up to request clarification or add additional context in comments.

1 Comment

Seems, I can work around the same. Let me give it a try

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.