MeshWorld India LogoMeshWorld.

Laravel Arr::flatten(): Flatten Nested Arrays

(Updated: Mar 27, 2026)
Listen to ArticleAI Speech
~5 min read narration
100%
Laravel Arr::flatten(): Flatten Nested Arrays

Arr::flatten() collapses a multi-dimensional PHP array into a single flat array. Laravel ships it as part of the Illuminate\Support\Arr class. It works at any nesting depth and takes an optional $depth parameter to control how many levels it digs. The result is always a re-indexed, one-dimensional array of values.

How do you use Arr::flatten() in Laravel?

Import the class first, then call the static method:

PHP
use Illuminate\Support\Arr;

$data = [
    'country' => 'India 🇮🇳',
    'languages' => [
        'Gujarati',
        'Hindi',
        'Sanskrit',
        'Tamil',
        'Urdu',
    ],
];

$flat = Arr::flatten($data);

Laravel extracts all values and returns them as a re-indexed array. The country key and languages key are gone — only their values remain:

TEXT
array:6 [
  0 => "India 🇮🇳"
  1 => "Gujarati"
  2 => "Hindi"
  3 => "Sanskrit"
  4 => "Tamil"
  5 => "Urdu"
]

The scenario: You’re pulling a list of available shipping zones from a config file. The config is nested by region and country. You need a flat list of zone names to pass to a <select> dropdown. Arr::flatten() gets you there in one call instead of a nested loop.

How does the depth parameter control flattening?

Without a depth argument, Arr::flatten() recurses all the way down. You can limit it. The $depth parameter specifies how many levels to unwrap.

This array has three levels of nesting. Flattening to depth 2 stops before the innermost [6, 7]:

PHP
use Illuminate\Support\Arr;

$data = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];

$partialFlat = Arr::flatten($data, 2);

At depth 2, the outer two levels collapse but the deepest [6, 7] pair stays intact:

TEXT
array:9 [
  0 => 12
  1 => 2
  2 => 4
  3 => 124
  4 => 7
  5 => 10
  6 => 8
  7 => 15
  8 => array:2 [
    0 => 6
    1 => 7
  ]
]

Passing INF (or omitting the argument entirely) flattens everything, including that last pair:

PHP
$fullyFlat = Arr::flatten($data, INF);
TEXT
array:10 [
  0 => 12
  1 => 2
  2 => 4
  3 => 124
  4 => 7
  5 => 10
  6 => 8
  7 => 15
  8 => 6
  9 => 7
]

How does Arr::flatten() compare to plain PHP alternatives?

PHP doesn’t have a built-in function for arbitrary-depth flattening. The closest native option is array_merge(...$array) with the splat operator — but it only works one level deep:

PHP
// Plain PHP: works only for one level of nesting
$nested = [[1, 2], [3, 4], [5, 6]];
$flat   = array_merge(...$nested);

// Result: [1, 2, 3, 4, 5, 6] — correct for this case

As soon as you have deeper nesting, array_merge(...$nested) either errors or only partially flattens:

PHP
$deep = [[1, [2, 3]], [4, [5, 6]]];
$flat = array_merge(...$deep);

// Result: [1, [2, 3], 4, [5, 6]] — inner arrays survive

Arr::flatten($deep) handles this correctly, digging all the way through. Use array_merge(...$nested) when you know your data is only one level deep and you want to avoid the dependency. Use Arr::flatten() for anything that might nest further.

See Flatten array to get IDs from an Eloquent collection if what you actually need is a flat list of IDs from a query result — there’s a more direct method for that case.

What’s the difference between Arr::flatten() and Arr::dot()?

Arr::flatten() drops keys and gives you a plain list of values. Arr::dot() keeps keys by converting nested keys into dot-notation strings:

PHP
use Illuminate\Support\Arr;

$data = [
    'user' => [
        'name' => 'Alice',
        'role' => 'admin',
    ],
];

// Arr::flatten — values only
Arr::flatten($data);
// ['Alice', 'admin']

// Arr::dot — key-value pairs preserved
Arr::dot($data);
// ['user.name' => 'Alice', 'user.role' => 'admin']

If you need to access nested config values, validate deep arrays, or build flat cache keys from structured data, Arr::dot() is the right tool. If you just need a list of values, Arr::flatten() is simpler.

Summary

  • Arr::flatten($array) collapses all nesting by default. Pass $depth to stop at a specific level.
  • Keys are always discarded. The result is a sequential integer-keyed array.
  • For key-preserving flattening, use Arr::dot().
  • Native array_merge(...$nested) works for single-level nesting only. Arr::flatten() handles arbitrary depth.
  • Requires importing Illuminate\Support\Arr. Works in Laravel 8 through 12.

FAQ

Does Arr::flatten() modify the original array? No. It returns a new array and leaves the input unchanged.

What happens if I pass an already-flat array? It returns a copy with re-indexed keys. No error, no change in values.

Does it work on Eloquent collection results? Not directly — Eloquent returns a Collection object, not a plain array. Call ->toArray() on the collection first, then pass the result to Arr::flatten().

Can I flatten an empty array? Yes. Arr::flatten([]) returns [] without errors.

Where is Arr::flatten() documented? In the Laravel 12 Helpers documentation under the Array Helpers section.

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive