# How to create a loop with array data from 2 functions?

**URL:** https://discourse.roots.io/t/how-to-create-a-loop-with-array-data-from-2-functions/16457
**Category:** sage
**Created:** 2019-08-26T07:10:34Z
**Posts:** 5

## Post 1 by @humantopus — 2019-08-26T07:10:34Z

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.

---

## Post 2 by @kero — 2019-08-26T07:41:10Z

Both your arrays use numbered indexes. You can create a [`for`](https://www.php.net/manual/en/control-structures.for.php) 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

---

## Post 3 by @humantopus — 2019-08-26T15:52:28Z

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 '$'`

---

## Post 4 by @humantopus — 2019-08-26T16:05:58Z

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

---

## Post 5 by @kero — 2019-08-26T16:42:31Z

Yeah, sorry about that. Corrected it in the answer

---

## Post 6 by @system — 2019-10-07T07:16:12Z

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