How to create a loop with array data from 2 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;

}

I’m sure there’s a much more elegant way to achieve this.

Both your arrays use numbered indexes. You can create a for loop to go through both at the same time like so

@php
$max = min(count($cargovolumes_cy), count($cargovolumes_ly));
@endphp
@for ($i = 0; $i < $max; $i++)
<div class="page_cargovolumes ly">
  {!! $cargovolumes_cy[$i] !!}
  <br>
  {!! $cargovolumes_ly[$i] !!}
</div>
@endfor

Or a bit shorter but (in my opinion) less readable

@for ($i = 0; $i < min(count($cargovolumes_cy), count($cargovolumes_ly)); $i++)
<div class="page_cargovolumes ly">
  {!! $cargovolumes_cy[$i] !!}
  <br>
  {!! $cargovolumes_ly[$i] !!}
</div>
@endfor

I’m using min() in both cases, so you don’t get an invalid index warning. Can you be sure that both arrays always have the same length? If not, there might be other solutions to better handle them having variable lengths

Not sure if I’m missing an additional edit for one of the scripts, but there’s a fatal error thrown with either of the two above, referring to the line before @endfor:

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: syntax error, unexpected '[', expecting variable (T_VARIABLE) or '{' or '$'

NM I see the error. There’s an extra [.

Yeah, sorry about that. Corrected it in the answer

1 Like

This topic was automatically closed after 42 days. New replies are no longer allowed.