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.
:::note[TL;DR]
Arr::flatten($array)flattens to a single level by default (depth = INF)- Pass a second argument
$depthto limit how many levels are collapsed - Array keys are discarded — only values are kept
- To preserve keys, use
Arr::dot()instead - Requires
use Illuminate\Support\Arr;at the top of your file :::
How do you use Arr::flatten() in Laravel?
Import the class first, then call the static method:
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:
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.
:::warning
Arr::flatten() discards all array keys. The output is always a numerically re-indexed array of values. If you need to preserve key-value pairs from a nested structure, use Arr::dot() instead — it converts nesting into dot-notation keys like languages.0, languages.1.
:::
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]:
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:
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:
$fullyFlat = Arr::flatten($data, INF);
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:
// 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:
$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:
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$depthto 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.
What to Read Next
- Create a Laravel Collection from an Array — once your array is flat, wrapping it in a collection opens up filter, map, and groupBy.
- Get an Array of IDs from an Eloquent Collection — a targeted method for extracting a flat list of IDs from query results.
- Remove Elements from a Laravel Collection — how to strip unwanted items after you’ve flattened and wrapped your data.