Stack Exchange
rearrange multidimensional array on basis of value of inner array and duplicated values
I have an array like this:
Array(
[0] => Array
    (
        [route_id] => 1539871200
        [booking_id] => 171
        [route] => Boattrip Destination 2
        [date] => 18 October 2018 
        [time] => 14:00 - 16:00
    )

[1] => Array
    (
        [route_id] => 1539856800
        [booking_id] => 170
        [route] => Boattrip Destination 2
        [date] => 18 October 2018 
        [time] => 10:00 - 12:00
    )

[2] => Array
    (
        [route_id] => 1539856800
        [booking_id] => 162
        [route] => Boattrip Destination 2
        [date] => 18 October 2018 
        [time] => 10:00 - 12:00
    )
)

Route_id's always share the same date, time and route values. Only thing that is different are the booking id's.
Now what I want the arrays to be sorted by route_id and then a create a new array having below structure
Array (

[1539871200] => Array
    (
    [route] => Boattrip Destination 2
    [date] => 18 October 2018 
    [time] => 14:00 - 16:00
    [booking_ids] => Array
        (                  
           [0] => 171               
        )
    )

[1539856800] => Array
    (
    [route] => Boattrip Destination 2
    [date] => 18 October 2018 
    [time] => 10:00 - 12:00
    [booking_ids] => Array
        (
            [0] => 170
            [1] => 162    
        )
    )
)

This was my attempt:
$output = array();
foreach($bookings as $item) {
    if(!isset($output[$item['route_id']])) {
        $output[$item['route_id']] = array();
    }
    $catName = $item['route_id'];
    unset($item['route_id']);
    $output[$catName][] = $item;
}

But that only groups the route_ids.

Try something like this:
$routes = [];
foreach ($yourArray as $booking)
{
    $bookingId = $booking['booking_id'];
    $routeId = $booking['route_id'];

    // Initialise an empty array set
    if (!array_key_exists($routeId, $routes)) {
        // Create a new array for booking IDs
        $booking['booking_ids'] = [];
        // Remove the IDs you don't want
        unset($booking['booking_id'], $booking['route_id']);

        // Push your booking data
        $routes[$routeId] = $booking;
    }

    // Push the current booking ID into an existing set
    $routes[$routeId]['booking_ids'][] = $bookingId;
}


I have delivered your exact desired output using best practices.   No unnecessary variables are generated. No unset() calls.  The only function in the snippet is ksort() and I'm not even sure you want it.
Code: (Demo)
foreach ($array as $set) {
    if (isset($result[$set["route_id"]])) {
        $result[$set["route_id"]]["booking_ids"][] = $set["booking_id"];  // push new element
    } else {
        $result[$set["route_id"]] = [
            "route" => $set["route_id"],            // new key name, transfer value
            "date" => $set["date"],                 // transfer key and value
            "time" => $set["time"],                 // transfer key and value
            "booking_ids" => [$set["booking_id"]]   // new key name, write value as first element
        ];
    }
}
ksort($result);  // ... you did say you wanted to sort them by route_id
var_export($result);

Output:
array (
  1539856800 => 
  array (
    'route' => 1539856800,
    'date' => '18 October 2018',
    'time' => '10:00 - 12:00',
    'booking_ids' => 
    array (
      0 => 170,
      1 => 162,
    ),
  ),
  1539871200 => 
  array (
    'route' => 1539871200,
    'date' => '18 October 2018',
    'time' => '14:00 - 16:00',
    'booking_ids' => 
    array (
      0 => 171,
    ),
  ),
)

