Laravel Tutorials

Extract Arrays from Any Data Type with Laravel's Arr::from Method

Published
Extract Arrays from Any Data Type with Laravel's Arr::from Method image

Laravel's new Arr::from method provides a unified approach to convert collections, objects, and other structures into arrays, handling various input types intelligently.

Convert different data types to arrays:

use Illuminate\Support\Arr;

// From collections
Arr::from(collect(['status' => 'active'])); // ['status' => 'active']

// From Jsonable objects
Arr::from($jsonable); // Decodes via Jsonable interface

// From Arrayable objects
Arr::from($arrayable); // Returns via toArray() method

Here's how you might use this in a configuration manager:

class ConfigurationManager
{
    public function processSettings($settings)
    {
        $array = Arr::from($settings);
        
        return $this->validateSettings($array);
    }
    
    public function combineConfigurations(...$configs)
    {
        return collect($configs)
            ->map(fn($config) => Arr::from($config))
            ->reduce(fn($carry, $item) => array_merge($carry, $item), []);
    }
    
    public function sanitizeAndStore($configuration, array $schema)
    {
        $data = Arr::from($configuration);
        
        $validator = Validator::make($data, $schema);
        
        if ($validator->fails()) {
            throw new ValidationException($validator);
        }
        
        return $data;
    }
}

$manager = new ConfigurationManager();

$result = $manager->processSettings(collect(['cache' => 'redis']));
$combined = $manager->combineConfigurations(
    collect(['database' => 'mysql']),
    new DatabaseConfig(),
    $cacheSettings
);

The Arr::from method provides a consistent interface for array extraction, eliminating the need to check data types or call different methods based on the input structure.

Harris Raftopoulos photo

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

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article