I have declared an array $data and passed it in the parameter for the foreach loop. inside the loop i need to push or merge the new array and return it. apparently the return object is not the as expected. it only contains the header. I'm using php7.2

$data = ['code','name'];
    $submission->contributions->each(function (Contribution $contribution) use ($data) {

        $rowData = [

        $data = array_merge($data, [$rowData]);

1 Answers

eselskas On Best Solutions

Could just use foreach, and your $data array will be available:

$data = ['code','name'];
foreach($submission->contributions as $contribution) {
    $data = array_merge($data,