2017-08-30 82 views
-1

我有一组时间范围的预订表。每个范围都会在数据库中生成一个新行。当我用特定的booking_id查询所有预订时,我得到4个数组中的所有结果。我想要做的是让他们列在一个booking_id下。如何在PHP中将数组元素分组并返回为JSON?

目前我得到这个JSON返回:

{ 
    "status":"success", 
    "data":[ 
     { 
      "first_name":"John", 
      "user_name":"john-k", 
      "booking_id":"3521AE", 
      "nanny_id":1, 
      "status":0, 
      "amount":"36.00", 
      "parent_id":15, 
      "date":"2017-07-02", 
      "start_time":"12:00:00", 
      "end_time":"13:00:00" 
     }, 
     { 
      "first_name":"John", 
      "user_name":"john-k", 
      "booking_id":"3521AE", 
      "nanny_id":1, 
      "status":0, 
      "amount":"36.00", 
      "parent_id":15, 
      "date":"2017-07-02", 
      "start_time":"13:00:00", 
      "end_time":"14:00:00" 
     }, 
     { 
      "first_name":"John", 
      "user_name":"john-k", 
      "booking_id":"3521AE", 
      "nanny_id":1, 
      "status":0, 
      "amount":"36.00", 
      "parent_id":15, 
      "date":"2017-07-02", 
      "start_time":"14:00:00", 
      "end_time":"15:00:00" 
     }, 
     { 
      "first_name":"John", 
      "user_name":"john-k", 
      "booking_id":"3521AE", 
      "nanny_id":1, 
      "status":0, 
      "amount":"36.00", 
      "parent_id":15, 
      "date":"2017-07-02", 
      "start_time":"15:00:00", 
      "end_time":"16:00:00" 
     } 
    ] 
} 

我想有是这样的:

{ 
    "status":"success", 
    "data":[ 
     { 
      "first_name":"John", 
      "user_name":"john-k", 
      "booking_id":"3521AE", 
      "nanny_id":1, 
      "status":0, 
      "amount":"36.00", 
      "parent_id":15, 
      "date":"2017-07-02", 
      "times":[ 
       { 
       "start_time":"12:00:00", 
       "end_time":"16:00:00" 
       } 
      ] 

     } 
    ] 
} 

我怎样才能做到这一点?我试图在我的Controller中使用foreach,通过booking_id对它们进行分组,但这根本不起作用。这里是我的控制器:

$booking_id = Input::get('booking_id'); 
    $booking_array = []; 
    $bookings = Nanny_bookings::selectRaw('first_name, user_name, nanny_bookings.booking_id, nanny_id, status, amount, nanny_bookings.parent_id, date, start_time, end_time') 
    ->join('booking_confirmations', 'booking_confirmations.booking_id', '=', 'nanny_bookings.booking_id') 
    ->join('users', 'users.id', '=', 'nanny_bookings.nanny_id') 
    ->where('nanny_bookings.booking_id', $booking_id) 
    ->groupBy('nanny_bookings.booking_id','nanny_id', 'nanny_bookings.parent_id', 'date', 'status', 'amount', 'start_time', 'end_time') 
    ->get(); 


    foreach ($bookings as $key => &$entry) { 
     $booking_array[$entry['booking_id']][$key] = $entry; 
    } 

    $response = array(
    'status' => 'success', 
    'data' => $booking 
); 

    //if request is ajax 
    if (Request::ajax()) { 
    return Response::json($response); 
    } 
+2

哪里'foreach'? – jeroen

+0

@ jeroen发布了它。 – raqulka

+0

为什么downvote?没有解释 – raqulka

回答

0

这可以是一个开始,替换你的实际foreach。

This code don't work if you have holes between times

// Function to check if a time is before or after another one 
function isOp($operator, $existingTime, $newTime) { 
    $newTimeInt = (int) str_replace(':', '', $newTime); 
    $existingTimeInt = (int) str_replace(':', '', $existingTime); 
    if ($operator === 'before') { 
     return ($newTimeInt < $existingTimeInt); 
    } else if ($operator === 'after') { 
     return ($newTimeInt > $existingTimeInt); 
    } 
} 


foreach ($bookings as $key => $entry) { 
    // Check if booking already exist 
    if (array_key_exists($entry['booking_id'], $booking_array)) { 
     // Booking already exist -> add time 
     if (isOp('before', $booking_array[$entry['booking_id']]['times'][0]['start_time'], $entry['start_time'])) { 
      $booking_array[$entry['booking_id']]['times'][0]['start_time'] = $entry['start_time']; 
     } 
     if (isOp('after', $booking_array[$entry['booking_id']]['times'][0]['end_time'], $entry['end_time'])) { 
      $booking_array[$entry['booking_id']]['times'][0]['end_time'] = $entry['end_time']; 
     } 
    } else { 
     // Create booking 
     $booking_array[$entry['booking_id']] = $entry; 
     // Remove start/end time, not needed on booking level 
     unset($booking_array[$entry['booking_id']]['start_time']); 
     unset($booking_array[$entry['booking_id']]['end_time']); 
     // Add first time 
     $booking_array[$entry['booking_id']]['times'] = array(
      array(
       "start_time"=>$entry["start_time"], 
       "end_time"=>$entry["start_time"] 
      ) 
     ); 
    } 
} 
+0

谢谢你的答案。我得到这个错误:App \ Models \ Dashboard \ Nanny \ Bookings \ Nanny_bookings的重载元素的间接修改没有任何效果 – raqulka

+0

Oups,foreach循环(错误字符)foreach中的一个小错误($ bookings as $ key => $ entry )',删除& – Camille

相关问题