Laravel Tutorials

Deep Array Manipulation with Laravel's replaceRecursive Method

Published Updated
Deep Array Manipulation with Laravel's replaceRecursive Method image

Laravel's replaceRecursive method offers a powerful solution for updating nested arrays while preserving unmodified values, particularly useful when working with complex configuration structures.

$collection = collect([
    'user' => [
        'name' => 'John',
        'settings' => [
            'theme' => 'dark',
            'notifications' => true,
        ]
    ]
]);

$updated = $collection->replaceRecursive([
    'user' => [
        'settings' => [
            'theme' => 'light'
        ]
    ]
]);

Let's explore a practical example with a configurable dashboard system:

<?php

namespace App\Services;
use Illuminate\Support\Collection;

class DashboardConfigurationService
{
    public function mergeUserPreferences(array $defaultConfig, array $userPreferences): array
    {
        return collect($defaultConfig)->replaceRecursive($userPreferences)->all();
    }

    public function getConfiguration(User $user): array
    {
        $defaultConfig = [
            'layout' => [
                'sidebar' => [
                    'position' => 'left',
                    'width' => 250,
                    'collapsed' => false
                ],
                'theme' => [
                    'mode' => 'light',
                    'color' => 'blue',
                    'font_size' => 14
                ],
                'widgets' => [
                    'weather' => true,
                    'calendar' => true,
                    'notifications' => true
                ]
            ]
        ];

        return $this->mergeUserPreferences(
            $defaultConfig,
            $user->dashboard_preferences ?? []
        );
    }
}

ReplaceRecursive provides an elegant way to handle deep array updates while maintaining unspecified values, perfect for managing configuration merges and preference systems.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article
PIE 1.5 Changes PHP Extension Installs in CI image

PIE 1.5 Changes PHP Extension Installs in CI

Read article
Delete Expired Sessions in Laravel With a Scheduled Command image

Delete Expired Sessions in Laravel With a Scheduled Command

Read article
Pollora: Run WordPress Inside a Laravel Application image

Pollora: Run WordPress Inside a Laravel Application

Read article
Scheduled Session Garbage Collection in Laravel 13.39 image

Scheduled Session Garbage Collection in Laravel 13.39

Read article