正常数组到嵌套的Json转换PHP

时间:2015-03-01 20:52:50

标签: php arrays json

我有一系列医院,医生正在工作日的不同时间访问 例如:

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
      )

    )

我希望以下JSON树格式的数组首先按医院排序,然后是时间,然后是时间:

    {
"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"
                    }
                ]
            }
        ]
    }
]
}

我尝试使用foreach循环进行细分但在医院级别之后无法解决问题。通用功能将有所帮助。

1 个答案:

答案 0 :(得分:1)

您需要浏览原始数组并创建最终数组,并根据需要进行排序。然后调用json_encode($finalArray, true)将为您提供json表示。

在您的示例中,您可以执行以下操作:

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