Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devkon98's avatar

How to merge two array together Laravel

Hello i am coding in laravel an excel export code and i have 2 arrays, one array gets the values from database the other array gets values from a foreach loop, i want to merge the values together but they aren't merging correctly. This is my code i have done until now:

foreach ($cashRegisterRecords as $cashRegisterRecord) {
            $arrayForExcel[] = [
                'Konfirmuar nga punojesi' => $cashRegisterRecord->employee_name,
                'Konfirmuar nga financa' => $cashRegisterRecord->finance_name,
                'Tipi veprimit' => $cashRegisterRecord->type_of_action,
                'Pershkrimi' => $cashRegisterRecord->description,
                'Imazhi' => $cashRegisterRecord->image_path != null ? $cashRegisterRecord->image_path : '------',
                'Vlera' => $this->getValues($cashRegisterRecord, $cashRegisterRecord->created_at),
                'Vlera e vendosur nga punonjesi' => $this->getValuesInsertedFromEmployee($cashRegisterRecord, $cashRegisterRecord->created_at),
                'Krijuar me' => Carbon::parse($cashRegisterRecord->created_at)->format('d-F-Y'),
                'Ndryshuar me' => Carbon::parse($cashRegisterRecord->updated_at)->format('d-F-Y'),

            ];
            $differenceValuesForExcel[] = $this->getDifferences($cashRegisterRecord, $allCurrencies);
        }
        
        $arrayForExcel = array_merge($arrayForExcel, $differenceValuesForExcel);

        return collect($arrayForExcel);


//This is the foreach code

   private function getDifferences($cashRegisterRecord, $allCurrencies)
    {
        foreach ($allCurrencies as $currency) {
            $arrayOfCurrencies[] = (unserialize($cashRegisterRecord->value_with_currencies)[$currency] ?? 0) - (unserialize($cashRegisterRecord->new_value)[$currency] ?? 0) . $currency;
        }
        return $arrayOfCurrencies;
    }
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To merge two arrays together in Laravel, you can use the array_merge function. However, in this case, the two arrays have different structures, so you need to make sure that you merge them correctly.

In the provided code, $arrayForExcel is an array of associative arrays, while $differenceValuesForExcel is an array of arrays. To merge them together, you need to append each array in $differenceValuesForExcel to the corresponding array in $arrayForExcel.

Here's an updated version of the code that should work:

foreach ($cashRegisterRecords as $cashRegisterRecord) {
    $arrayForExcel[] = [
        'Konfirmuar nga punojesi' => $cashRegisterRecord->employee_name,
        'Konfirmuar nga financa' => $cashRegisterRecord->finance_name,
        'Tipi veprimit' => $cashRegisterRecord->type_of_action,
        'Pershkrimi' => $cashRegisterRecord->description,
        'Imazhi' => $cashRegisterRecord->image_path != null ? $cashRegisterRecord->image_path : '------',
        'Vlera' => $this->getValues($cashRegisterRecord, $cashRegisterRecord->created_at),
        'Vlera e vendosur nga punonjesi' => $this->getValuesInsertedFromEmployee($cashRegisterRecord, $cashRegisterRecord->created_at),
        'Krijuar me' => Carbon::parse($cashRegisterRecord->created_at)->format('d-F-Y'),
        'Ndryshuar me' => Carbon::parse($cashRegisterRecord->updated_at)->format('d-F-Y'),
    ];
    $differenceValuesForExcel[] = $this->getDifferences($cashRegisterRecord, $allCurrencies);
}

// Merge the arrays
for ($i = 0; $i < count($arrayForExcel); $i++) {
    $arrayForExcel[$i] = array_merge($arrayForExcel[$i], $differenceValuesForExcel[$i]);
}

return collect($arrayForExcel);

In this updated code, we first loop through $cashRegisterRecords and build $arrayForExcel and $differenceValuesForExcel as before. Then, we loop through $arrayForExcel and use array_merge to append each array in $differenceValuesForExcel to the corresponding array in $arrayForExcel.

Note that this assumes that $cashRegisterRecords, $arrayForExcel, and $differenceValuesForExcel all have the same number of elements. If that's not the case, you'll need to modify the code accordingly.

Please or to participate in this conversation.