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

charlieBrown's avatar

Update an array inside another array using array_splice

I'm using Laravel chunk() method to build a grid system. I would like to place an ad on a rand column and for that I'm using this call:

        // We will split into chunks to create the ads placement
        $giveaways = $giveaways->chunk(2);



        foreach($giveaways as $chunk){
            $chunk = $chunk->toArray();
            array_splice($chunk, $randInt, 0, 'ADS');
            dd($chunk); <-- Here I can see that the ad is inserted
        }

        dd($giveaways); <-- Here the "ADS" is not present anymore

As you can see from the code the ADS is not inserted after it leaves the foreach. How I can update the $chunk array so that when it leaves the foreach it leaves updated with three items per chunk?

Thank you for your patience and time for answering the question

0 likes
3 replies
lostdreamer_nl's avatar

You're changing the $chunk in the loop, but never put it back into the $giveaways.

        foreach($giveaways as $key => $chunk){
            $chunk = $chunk->toArray();
            array_splice($chunk, $randInt, 0, 'ADS');
            // put the changed chunck back into the $giveaways
            $giveaways[$key] = $chunk;
        }

Or you can loop over the $giveaways array this way passing it by reference inside the loop:

        foreach($giveaways as $key => &$chunk){ // Note the & before $chunk
            $chunk = $chunk->toArray();
            array_splice($chunk, $randInt, 0, 'ADS');
        }

more info about pass-by-reference: http://php.net/manual/en/language.references.pass.php

1 like
charlieBrown's avatar

@lostdreamer_nl Thank you for your answer. Regarding your first option I tried it but it returns an array and not the collection itself. I need the collection because in the view I'm using a method contained in the Giveaway Model

$authorizedAppsNeeded = $giveaway->authorizedAppNeeded(array_unique($giveaway->tasks->pluck('taskType')->all()))

Second option I have no idea but the $giveaway is not updated even passing it as reference.

After sometime I have approached the problem different:

         // We will split into chunks to create the ads placement
        $giveaways = $giveaways->chunk(2);

        foreach($giveaways as &$chunk){
            $chunk->push('ADS');
        }

Don't ask me why it is beeing updated now by reference since using array_splice is not beeing updated

And then at the view I shuffle the array

@foreach($giveaways as $chunk)
                <?php $chunck = $chunk->shuffle(); ?>
                ....

But the collection is not beeing shuffled. If I happen to do dd($giveaway[0]->shuffle()) in the controller I can see that the collection gets shuffled but not in the view.

Any idea what is happening here?

charlieBrown's avatar
<?php $chunck = $chunk->shuffle(); ?>
// I changed to
<?php $shuffled = $chunk->shuffle(); ?>

Now it is working. Thank you for your help @lostdreamer_nl . The reference one really helped me out

Please or to participate in this conversation.