Question: How would I merge x number of arrays created in a loop like this:

```
$pageCount = // a dynamic number

for ($i = 0; $i <= $pageCount; $i++) {
  $events = get_events($search_query, $i);
}

$totalMergedEvents = ??

get_events($query, $page) {
  // api call 
  // $eventData = json_decode($output)
  // return $eventData 
}

```

Answer: 
If you have array of events `$events = []`.

```
$events = [];

for ($i = 0; $i <= $pageCount; $i++) {
  $events[] = get_events($search_query, $i);
}

```

You can simply flatten the array.

```
$eventData = array_reduce($events, function($arr, $element) {
    $arr[] = $element;
    return $arr;
});

```
If you proceed this way:

```
for ($i = 0; $i <= $pageCount; $i++) {
  $events = get_events($search_query, $i);
}

```

When the loop is completed you will get only the last data fetch by the API as you are overriding the data saved in `$events`. Exemple:

```
$event = "a";
$event = "b";

echo $event; // will equal to b.

```

So you will have to proceed like this. Create an empty array first. Then push the data return by the API like below:

```
$pageCount = // a dynamic number
$events = []; // Your array to hold data from API call

for ($i = 0; $i <= $pageCount; $i++) {
  // Everytime the api return data, il will be push to the array.
  $events[] = get_events($search_query, $i);
}

$totalMergedEvents = ??

get_events($query, $page) {
  // api call 
  // $eventData = json_decode($output)
  // return $eventData 
}

```

var\_dump the $events variable to check if the data is present.

```
var_dump($events):

```