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 = [
            $contribution->employment->employer->code,
            $contribution->employment->employee->last_name,
        ];

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

1 Answers

1
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, 
        [
            $contribution->employment->employer->code,
            $contribution->employment->employee->last_name
        ]);
}