Returning JSON depending on the route_id I have a controller that has a purpose of returning json depending on the route_id being passed from the route (Using laravel). There's a point in my code where I resorted to using a double nested foreach loop in order to iterate the values I want to be stored into an array. Everything works fine, but I feel like the code is messy. Any tips?

public function getRoute($id)
{
    $route = Route_log::find($id);      
    $allWayPoints = explode(";", $route-&gt;route_order);
    $firstWaypoint = array_shift($allWayPoints); //get first waypoint

    foreach($allWayPoints as $allWayPoint)
        {
            $allOrdersInWaypoint = Transaction::where('waypoint_id', "=", $allWayPoint)-&gt;where('type', "=", "DD")-&gt;get()-&gt;lists('order_id');
            $allOrders[] = array($allOrdersInWaypoint);
        }
  ///**THIS PART**
    foreach($allOrders as $allOrder)
    {
        foreach($allOrder as $aO)
        {
            foreach($aO as $a)
            {
                $otherOrders = Order::find($a);

                if(isset($otherOrders))
                {
                $getOtherOrders[] = array(
                                            'id'             =&gt; $otherOrders-&gt;id,
                                            'tracking_id'    =&gt; $otherOrders-&gt;tracking_id,
                                            'shipper_ref_no' =&gt; $otherOrders-&gt;shipper_ref_no);
                }
            }
        }
    }

    //get all transactions within the first way point
    $ordersInWaypoint = array();
    $ordersInWaypoint = Transaction::where('waypoint_id', "=", $firstWaypoint)-&gt;where('type', "=", "PP")-&gt;get()-&gt;lists('order_id');

    //get order id, tracking_id, and shipper ref no and store into an array
    foreach($ordersInWaypoint as $ordersInWaypoints)
        {
            $order = Order::find($ordersInWaypoints);

            //this array stores id, trackingId, and shipper_ref_no per order
            $getOrder[] = array(
                                'id'             =&gt; $order-&gt;id,
                                'tracking_id'    =&gt; $order-&gt;tracking_id,
                                'shipper_ref_no' =&gt; $order-&gt;shipper_ref_no);    
        }

    $json = (array('first_waypoint' =&gt; $getOrder, 'remaining_waypoints' =&gt; $getOtherOrders));

    return $json;
}

}


To show an example array inside $allOrders:

 array (size=20)
   0 =&gt; 
     array (size=1)
      0 =&gt; 
         array (size=2)
           0 =&gt; int 491
           1 =&gt; int 494
   1 =&gt; 
     array (size=1)
       0 =&gt; 
         array (size=0)
           empty
   2 =&gt; 
     array (size=1)
       0 =&gt; 
         array (size=1)
           0 =&gt; int 498
   3 =&gt; 
     array (size=1)
       0 =&gt; 
         array (size=2)
           0 =&gt; int 487
           1 =&gt; int 493
   4 =&gt; 
     array (size=1)
       0 =&gt; 
         array (size=1)
           0 =&gt; int 503
   5 =&gt; 
     array (size=1)
       0 =&gt; 
         array (size=1)
           0 =&gt; int 480
   6 =&gt; 
     array (size=1)
       0 =&gt; 
         array (size=1)
           0 =&gt; int 476
   7 =&gt; 
     array (size=1)
       0 =&gt; 
         array (size=1)
           0 =&gt; int 482
   8 =&gt; 
     array (size=1)
       0 =&gt; 
         array (size=1)
           0 =&gt; int 515
How about somethink like:

 $allOrders = array();
 foreach($allWayPoints as $allWayPoint)
 {
            $allOrdersInWaypoint = Transaction::where('waypoint_id', "=", $allWayPoint)-&gt;where('type', "=", "DD")-&gt;get()-&gt;lists('order_id');
 //           $allOrders[] = array($allOrdersInWaypoint);
            $allOrders = array_merge($allOrders, $allOrdersInWaypoint);
 }

 foreach($allOrders as $order)
 {
       $otherOrders = Order::find($order);

       if(isset($otherOrders))
       {
            $getOtherOrders[] = array(
                                            'id'             =&gt; $otherOrders-&gt;id,
                                            'tracking_id'    =&gt; $otherOrders-&gt;tracking_id,
                                            'shipper_ref_no' =&gt; $otherOrders-&gt;shipper_ref_no);
       }
 }


Then they wont be nested and $allOrders will simply be a flat array containing the numbers, assuming $allOrdersInWaypoint is a array of numbers.