PHP根据条件比较关联数组 [英] PHP compare associative array based on condition
问题描述
我有以下数组的输出
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] =>
)
)
因此,在这个数组中,我试图根据满足以下条件的条件对数组进行分组
1)提取buses
,其中stop_name=Stop_k
和stop_name=Stop_s
和trip
和bus_direction
在stop_name=Stop_k
和stop_name=Stop_s
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();
}
}
任何人都可以帮助我实现它.谢谢. 由于我尝试使用查询获取结果但未能成功,因此我已经对此有一个疑问 MYSQL AND查询以满足同一列
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
更新
我在laravel中的查询
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();
我也尝试过以下查询
$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
如果我谈论数组结果,则需要基于少数条件将总线分组,例如相同的bus_name必须同时具有[stop_name] => Stop_8
和[stop_name] => Stop_18
并且[trip]
和[bus_direction]
在两个[stop_name] => Stop_8
中必须相同[stop_name] => Stop_18
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
此处更新了问题
此处链接为sql文件
https://github.com/codeforfungit/bustiming
推荐答案
尝试一下:
$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')
->where([['trip','=','bus_direction'],['stops.stop_name','=',$Stop_k]])
->orWhere([['trip','=','bus_direction'],['stops.stop_name','=',$Stop_s])
->get();
当然可以更好,但是就目前而言,虽然我没有足够的信息,但这还不错!
of course it could be better but for now and while i don't have enough information it's not bad!
这篇关于PHP根据条件比较关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!