Arr::flatten

Flattens a nested array into a single-level list, discarding every key at every depth.

Signature
public static function flatten($array, $depth = INF)
  • Added in Laravel 5.0

Parameters

ParameterTypeDescription
$$arrayiterableThe array or iterable to flatten. Nested Collection instances are unwrapped with all().
$$depthoptionalint= INFHow many levels of nesting to remove. INF removes every level.

Return value

arrayA sequential, zero-indexed array of the values found, with all keys discarded.

Arr::flatten walks the array recursively and appends every non-array value it finds to one flat, zero-indexed list. Keys are not merged or preserved — they are discarded at every level, including the top one. If you need the paths instead, that is Arr::dot; if you want to stop after a single level and keep string keys, that is Arr::collapse.

When to reach for it

Use it to gather leaf values regardless of shape: collecting every tag across a set of posts, pulling all IDs out of a grouped result, or building the argument list for a whereIn from nested chunks. The $depth argument makes it a controlled operation rather than an all-or-nothing one.

What trips people up

The keys really are gone, so flattening an associative array gives you values only and there is no way to tell which key a value came from afterwards. Duplicates are kept — flatten does not deduplicate. And $depth counts levels removed, not levels kept: flatten($array, 1) unwraps one level and leaves anything deeper as an array value in the result.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Collecting every tag across a grouped set of posts into one list.
  • Extracting all IDs from a nested or chunked result before a whereIn query.
  • Reducing a tree of nested option arrays to the flat list a select field needs.

Frequently asked questions

Does Arr::flatten keep array keys?

No, every key is discarded at every level and the result is always a zero-indexed list. Use Arr::dot if you need the key path preserved.

How do I flatten only one level?

Pass 1 as the second argument, or use Arr::collapse, which removes exactly one level and keeps the string keys of the inner arrays.

Does Arr::flatten remove duplicate values?

No, duplicates are preserved in the order they were found; wrap the result in array_unique and array_values if you need a distinct list.

More examples

Gathering unique tags

Prints ["laravel","php","testing"]. Flatten collapses the per-post tag arrays into one list including the duplicate 'laravel', so array_unique is still needed; sort makes the output stable no matter what order the posts arrive in.

Example · php
<?php

use Illuminate\Support\Arr;

$posts = [
    ['title' => 'A', 'tags' => ['php', 'laravel']],
    ['title' => 'B', 'tags' => ['laravel', 'testing']],
];

$tags = array_values(array_unique(Arr::flatten(array_column($posts, 'tags'))));
sort($tags);

echo json_encode($tags), "\n";

Flatten versus dot versus collapse

Prints ["Ada","admin"], then {"user.name":"Ada","user.roles.0":"admin"}, then {"name":"Ada","roles":["admin"]}. Flatten keeps only values, dot keeps the full path as a key, and collapse removes exactly one level while preserving the string keys underneath it.

Example · php
<?php

use Illuminate\Support\Arr;

$data = ['user' => ['name' => 'Ada', 'roles' => ['admin']]];

echo json_encode(Arr::flatten($data)), "\n";
echo json_encode(Arr::dot($data)), "\n";
echo json_encode(Arr::collapse($data)), "\n";

Controlled depth on a tree

Prints [{"label":"Home"},{"label":"Docs"},{"label":"API"}] then ["Home","Docs","API"]. With a depth of 1 the item arrays survive as values, which is usually what you want; the unlimited default keeps going and dissolves the items themselves into their labels.

Example · php
<?php

use Illuminate\Support\Arr;

$menu = [
    ['label' => 'Home'],
    [['label' => 'Docs'], ['label' => 'API']],
];

echo json_encode(Arr::flatten($menu, 1)), "\n";
echo json_encode(Arr::flatten($menu)), "\n";

Discussion

  • Be the first to comment on this lesson.