I have following output of array
Array
(
[0] => Array
(
[id] => 1
[bus_id] => 1
[stop_order_id] => 14
[bus_time] => 09:59:00
[bus_direction] => 1
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 1
[stop_id] => 8
[stop_orders] => 14
[route_name] => Sydney
[seo_url] => sydney
[stop_name] => stop_8
[bus_name] => M.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
[1] => Array
(
[id] => 1
[bus_id] => 1
[stop_order_id] => 22
[bus_time] => 10:50:00
[bus_direction] => 1
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 1
[stop_id] => 18
[stop_orders] => 22
[route_name] => Sydney
[seo_url] => sydney
[stop_name] => stop_18
[bus_name] => M.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
[2] => Array
(
[id] => 1
[bus_id] => 1
[stop_order_id] => 22
[bus_time] => 10:55:00
[bus_direction] => 2
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 1
[stop_id] => 18
[stop_orders] => 22
[route_name] => Sydney
[seo_url] => sydney
[stop_name] => stop_18
[bus_name] => M.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
[3] => Array
(
[id] => 1
[bus_id] => 1
[stop_order_id] => 14
[bus_time] => 11:58:00
[bus_direction] => 2
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 1
[stop_id] => 8
[stop_orders] => 14
[route_name] => Sydney
[seo_url] => sydney
[stop_name] => stop_8
[bus_name] => M.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
[4] => Array
(
[id] => 2
[bus_id] => 2
[stop_order_id] => 14
[bus_time] => 10:05:00
[bus_direction] => 1
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 1
[stop_id] => 8
[stop_orders] => 14
[route_name] => Sydney
[seo_url] => sydney
[stop_name] => stop_8
[bus_name] => J.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
[5] => Array
(
[id] => 2
[bus_id] => 2
[stop_order_id] => 22
[bus_time] => 10:55:00
[bus_direction] => 1
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 1
[stop_id] => 18
[stop_orders] => 22
[route_name] => Sydney
[seo_url] => sydney
[stop_name] => stop_18
[bus_name] => J.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
[6] => Array
(
[id] => 2
[bus_id] => 2
[stop_order_id] => 22
[bus_time] => 10:58:00
[bus_direction] => 2
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 1
[stop_id] => 18
[stop_orders] => 22
[route_name] => Sydney
[seo_url] => sydney
[stop_name] => stop_18
[bus_name] => J.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
[7] => Array
(
[id] => 5
[bus_id] => 5
[stop_order_id] => 27
[bus_time] => 11:07:00
[bus_direction] => 1
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 2
[stop_id] => 8
[stop_orders] => 5
[route_name] => Melbone
[seo_url] => Melbone
[stop_name] => stop_8
[bus_name] => R.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
[8] => Array
(
[id] => 5
[bus_id] => 5
[stop_order_id] => 32
[bus_time] => 12:50:00
[bus_direction] => 1
[trip] => 1
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
[route_id] => 2
[stop_id] => 18
[stop_orders] => 10
[route_name] => Melbone
[seo_url] => Melbone
[stop_name] => stop_18
[bus_name] => R.S
[bus_number] =>
[bus_image] =>
[bus_stop_start] =>
[bus_stop_end] =>
)
)
So in this array i am trying to group arrays based on few condition satisfy
1) fetch buses which as stop_name=Stop_k and stop_name=Stop_s and trip and bus_direction must same in both stop_name=Stop_k and stop_name=Stop_s
I have tired to make new array but looks like its become complicated for me
$busGroup=[];
foreach ($response as $key=>$value){
if(!in_array($value->bus_name,$busGroup)){
$busGroup[$value->bus_name][$value->trip][$value->bus_direction][]=$value->toArray();
}
}
Can any one help me to make it works .Thank you. Since i tried to get result using query but failed to make it.I had already one question regarding this MYSQL AND query to satisfy on same column
Even if i get same result in query also i am okay with it
Update
my query in laravel
$response=BusTimingModel::join('stop_orders','stop_orders.id','=','bus_timings.stop_order_id')
->join('routes','stop_orders.route_id','=','routes.id')
->join('stops','stop_orders.stop_id','=','stops.id')
->join('buses','buses.id','=','bus_timings.bus_id')
->whereIn('stops.stop_name',['stop_8', 'Stop_18'])
->get();
I have tried following query also
$response=BusTimingModel::join('stop_orders','bus_timings.stop_order_id','=','stop_orders.id')
->join('routes','stop_orders.route_id','=','routes.id')
->join('stops','stop_orders.stop_id','=','stops.id')
->join('buses','buses.id','=','bus_timings.bus_id')
->whereIn('stops.stop_name',['stop_8', 'stop_18'])
->havingRaw('bus_timings.bus_time BETWEEN CAST("01:59:00" AS time) AND CAST("12:00:00" AS time)')
->get();
Expected Result when user search between source to destination along with the time then application should return all buses travel between source and destination (if user select long time for example
if i speak about array result i need to group by buses based on few condition such as same bus_name must have both [stop_name] => Stop_8 AND [stop_name] => Stop_18 AND [trip] and [bus_direction] must be same in both [stop_name] => Stop_8 AND [stop_name] => Stop_18
Updated Question here
Link attached here for sql file