1

I am looking for a way to flatten this array:

$array = ['parent' => ['childA' => "value", 'childB' => "value"]];

to:

$array = ['parent.childA' => "value", 'parent.childB' => "value"];

Is there an elegant way to do this in Laravel? For example with Laravel Collections.

2 Answers 2

3

The array_dot() Laravel helper is done exactly for that:

array_dot($array);

The array_dot function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth

Sign up to request clarification or add additional context in comments.

Comments

3

For Laravel 5.7 and newer:

take a look at Arr::dot()

For Laravel 5.6 and older:

array_dot is the way to go (see Alexeys answer)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.