如何从php中的数组中删除关联数组

时间:2018-03-01 23:06:19

标签: php arrays multidimensional-array associative-array

我在邮递员中有以下回应

"InnerData": [
        {},
        {
            "id": 2,
            "time": "9:00 am - 10:00 am"
        },
    ]

我想删除空数组,我在数组上尝试了unset()和array_splice,从而产生了这个响应,但结果却是

"InnerData": {
        "0": {
            "id": 2,
            "time": "9:00 am - 10:00 am"
        },
        "1": {
            "id": 3,
            "time": "10:00 am - 11:00 am"
        }
   }

我不希望数组有像0和1

这样的键

我的php ode就像

public function getStadiumAvailableTimes(Request $request){
    $id = $request->stadium_id;
    $date = $request->current_date;
    $stadiumObject = \App\Place::where(['id' => $id, 'place_type' => 3])->get();
    $availableTimes = [];
    // return response()->json($stadiumObject, 200);
    $counter = 1;
    foreach($stadiumObject as $key => $value){
        $first = strtotime($value->start_date);
        $firstTime = date('H:i', $first);
        $last = strtotime($value->end_date);
        $lastTime = date('H:i', $last);
        $diff = intval($lastTime)-intval($firstTime);
        for($i = 0 ; $i < $diff ; $i++){
            $availableTimes[$i]['id'] = $counter;
            $ti = strtotime($value->start_date)+ 60*60*$i;
            $ti2 = strtotime($value->start_date) + 60*60*($i+1);
            $availableTimes[$i]['time'] = date('g:i a', $ti).' - '.date('g:i a',$ti2);
            if (\App\Reservation::where(['date' => $date, 'starting' => date('g:i', $ti)])->count() > 0) {
                unset($availableTimes[$i]['time']);
                unset($availableTimes[$i]['id']);
                $availableTimes[$i] = (object)[];
                array_splie($availableTimes,$i);

            }
            $counter++;
        }



    }
    $resp = new \App\Http\Helpers\ServiceResponse;

    $resp->Message = "Successfull.";

    $resp->Status = true;

    $resp->InnerData = $availableTimes;
    return response()->json($resp,200);
}

0 个答案:

没有答案