wordpresslaravelmariadbroots-sage

Laravel foreach with 2 key/pair functions


I have multiple arrays with similar key/pair sets. I'd like to loop through them using a foreach and print 2 per page element.

Sets:

 <div class="page_cargovolumes cy_detail">
  @foreach ($cargovolumes_cy as $cargos_cy)
   {!! $cargos_cy !!}
  @endforeach
</div>
<div class="page_cargovolumes ly">
 @foreach ($cargovolumes_ly as $cargos_ly)
  {!! $cargos_ly !!}
 @endforeach
</div>

How can I use the loop output of both, into 1 element containing both?

Desired result:

<div class="page_cargovolumes ly">
  {!! $cargos_cy !!}
  <br>
  {!! $cargos_ly !!}
</div>

Ex. db data below:

public function cargovolumes_cy() {
    global $wpdb;

    $cargodb = new $wpdb('root', 'expw', 'exdb', 'localhost');
    $currentmonthtxt = date('Y');
    $currentyear = date('Y');
    $cargovolume_cy = array();

    foreach($cargodb->get_results(
        "
        SELECT    *, SUM(tonneCount)
        FROM      volumes
        WHERE year = $currentyear
        GROUP BY terminal, year, month
        "
    )  as $key => $row) {
        $tonnages = $row->tonneCount;
        $terminal = $row->terminal;
        $year = $row->year;
        $month = $row->month;
        $cargovolume_cy[] = 
        '<div class="cargovolumes_cy"><h4>'.$terminal.' </h4>'. 
        '<span class="cv-year">Year: '.$year.' </span>'. 
        '<span class="cv-month"> Month: '.$month.' </span>'. 
        '<span class="cv-tonnage"> Tonnage: '.$tonnages.' </span></div>';
    };

    return $cargovolume_cy;

}

public function cargovolumes_ly() {
    global $wpdb;

    $cargodb = new $wpdb('root', 'expw', 'exdb', 'localhost');
    $currentmonthtxt = date('Y');
    $currentyear = date('Y');
    $cargovolume_ly = array();

    foreach($cargodb->get_results(
        "
        SELECT    *, SUM(tonneCount)
        FROM      volumes
        WHERE year = $currentyear -1
        GROUP BY terminal, year, month
        "
    )  as $key => $row) {
        $tonnages = $row->tonneCount;
        $terminal = $row->terminal;
        $year = $row->year;
        $month = $row->month;
        $cargovolume_ly[] = 
        '<div class="cargovolumes_ly">'. 
        '<span class="cv-year">Year: '.$year.' </span>'. 
        '<span class="cv-month"> Month: '.$month.' </span>'. 
        '<span class="cv-tonnage"> Tonnage: '.$tonnages.' </span></div>';
    };

    return $cargovolume_ly;

}

Solution

  • foreach operates only one array at a time there are two way out to get your desired result.

    Wayout out 1

    <div class="page_cargovolumes ly">
    @foreach (array_combine($cargovolumes_cy, $cargovolumes_ly) as $cargos_cy => $cargos_ly) 
       {!! $cargos_cy !!}
          <br>
       {!! $cargos_ly !!}
    @endforeach
    </div>
    

    Wayout out 2

    <div class="page_cargovolumes ly">
    @foreach($cargovolumes_cy as $nIndex => $cargos_cy)
       {!! $cargos_cy !!}
          <br>
       {!! $cargovolumes_ly[$nIndex] !!}
    @endforeach
    </div>