1

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

https://github.com/codeforfungit/bustiming

8
  • where are the results come form? i guess they are coming from db. Commented Jan 12, 2018 at 18:02
  • @Amin.Ya you are right.i will update my query in question. Commented Jan 12, 2018 at 18:02
  • It would be nice if you post an example of an expected result. Commented Jan 12, 2018 at 18:04
  • @Laerte.Ya i have updated my question Commented Jan 12, 2018 at 18:09
  • what are Stop_k and Stop_s. Commented Jan 12, 2018 at 18:09

1 Answer 1

1

try this:

$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!

Sign up to request clarification or add additional context in comments.

12 Comments

@Amin.Thanks for the effort for anwer.in where condtion i cant hard code bus direction since one bus have multiple trips in a day and every trip will have direction(here direction means melborne to sydney 1 and sydney to melborne 2 it means visa versa)
@if i remove trip in where condition then i get same result which i posted if you need more detail you can check my previous question.Thank you so much for the effort
get $Stop_s and $Stop_k form user. it's not hard coding!
@Amin.Yes you are right but said about trip','=','bus_direction' i cant hard code
sorry! if i made mistake it corresponds to my insufficient information from your database. any way you can operate on array. although that would be wrong way. in a for loop check if direction = trip and if not unset the member form array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.