PHP compares associative array based on condition

I have the following array output

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'm trying to be grouped arrays on the basis of several conditions, satisfying 1) choose buseswhich like stop_name=Stop_kand stop_name=Stop_sand tripand bus_directionshould be the same in both stop_name=Stop_k, and sostop_name=Stop_s

I'm tired of creating a new array, but it looks like it has 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 someone help me make it work. Thank you. Since I tried to get the result using the query, but could not do it. I already had one question regarding this MYSQL AND query to satisfy in one column

Even if I get the same result in the query, and I'm fine with it

Update

my request 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 also tried the following query

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

, , ( ,

, , _ [stop_name] => Stop_8 AND [stop_name] => Stop_18 AND [trip], [bus_direction] [stop_name] => Stop_8 AND [stop_name] => Stop_18

, sql

https://github.com/codeforfungit/bustiming

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

, , , !

0

Source: https://habr.com/ru/post/1742024/


All Articles