PHP比较基于条件的关联数组

问题描述 投票:1回答:1

我有以下数组输出

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_kstop_name=Stop_s以及tripbus_directionstop_name=Stop_kstop_name=Stop_s都必须相同

我已经厌倦了制造新的阵列,但看起来它对我来说变得复杂了

  $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 query to satisfy on same column的问题

即使我在查询中得到相同的结果,我也没关系

更新

我在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();

预期结果当用户在源到目的地之间搜索以及应用程序的时间应该返回源和目的地之间的所有公共汽车旅行(如果用户选择长时间,例如

如果我谈论阵列结果我需要基于几个条件的总线分组,如相同的bus_name必须有[stop_name] => Stop_8[stop_name] => Stop_18[trip][bus_direction]必须相同的[stop_name] => Stop_8[stop_name] => Stop_18

此处更新了问题

此处附带的链接用于sql文件

https://github.com/codeforfungit/bustiming

php mysql arrays laravel
1个回答
1
投票

试试这个:

$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();

当然它可能会更好但是现在虽然我没有足够的信息但它还不错!

© www.soinside.com 2019 - 2024. All rights reserved.