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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article