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

devkon98's avatar

How to use foreach for array Laravel

Hello i have this code in Laravel that a foreach is used to fill an array, but doesnt work exactly like i want it to, this is the code:

$currencies = CurrencyResource::collection(Currency::all());

  foreach ($currencies as $currency){
                        $cashReceivedWithZeroValues [] = (object)[
                            'currency' => serialize([
                                $currency->name => '0',
                            ]),
                            'date' => $dateRangeFormat
                        ];
                    }


return response()->json([
            'message' => 'Success',
            'data' => [
                'allCurrencies' => $currencies,
                'graphData' => OutlayResource::collection($cashReceivedWithZeroValues),
            ]
        ], 200);

This is the result in Json i get:

{
                "currencies": {
                    "ALL": 0
                },
                "date": "2023-02-15"
            },
            {
                "currencies": {
                    "USD": 0
                },
                "date": "2023-02-15"
            },
            {
                "currencies": {
                    "EUR": 0
                },
                "date": "2023-02-15"
            },
            {
                "currencies": {
                    "GBP": 0
                },
                "date": "2023-02-15"
            },
            {
                "currencies": {
                    "Test": 0
                },
                "date": "2023-02-15"
            },

What i need is to show like this:

   "currencies": {
                    "ALL": 0,
					"USD": 0,
					"EUR": 0,
					"GBP": 0,
					"TEST": 0,
                },
                "date": "2023-02-15"
            },

So basically all currencies under 1 array not separated.

0 likes
2 replies
LaryAI's avatar
Level 58

You can use the array_merge() function to combine the arrays into one. Try the following code:

$currencies = CurrencyResource::collection(Currency::all());
$cashReceivedWithZeroValues = [];

foreach ($currencies as $currency){
    $cashReceivedWithZeroValues[] = (object)[
        'currency' => serialize([
            $currency->name => '0',
        ]),
        'date' => $dateRangeFormat
    ];
}

$cashReceivedWithZeroValues = array_merge(...$cashReceivedWithZeroValues);

return response()->json([
    'message' => 'Success',
    'data' => [
        'allCurrencies' => $currencies,
        'graphData' => OutlayResource::collection($cashReceivedWithZeroValues),
    ]
], 200);
kokoshneta's avatar

If you want just one object, why are you creating an array for each iteration and saving it as a separate array item? And why the serialising?

Just do this:

$cashReceivedWithZeroValues = (object) ['date' => $dateRangeFormat];
$currencies = Currencies::all();
$currencies->each(fn ($item) => $cashReceivedWithZeroValues->{$item->name} = '0');
$currencies = CurrencyResource::collection($currencies);

return response()->json([
	'message' => 'Success',
	'data' => [
		'allCurrencies' => $currencies,
		'graphData' => OutlayResource::collection($cashReceivedWithZeroValues),
	]
], 200);

Please or to participate in this conversation.