Laravel Collection Flatten Method Example

Laravel Collection Flatten Method Example

In this post we will give you information about Laravel Collection Flatten Method Example. Hear we will give you detail about Laravel Collection Flatten Method ExampleAnd how to use it also give you demo for it if it is necessary.

In this article we will cover on how to implement laravel collection flatten example. if you want to see example of laravel collection flatten one level then you are a right place. i would like to share with you laravel collection flatten array. you can see laravel eloquent flatten.

The flatten method will help to convert a multi-dimensional collection into a single dimension.

I will give you simple examples of flatten colletion in laravel. so you can easily use it with your laravel 5, laravel 6 and laravel 7 application. so let’s see bellow example that will helps you lot.

Syntax:

$collecton->flatten(

$depth INT

);

Laravel Collection flatten() Example

public function index()

{

$collection = collect([

'name' => 'Hardik',

'role' => ['admin', 'manager', 'superadmin'],

'multi' =>

[

'one',

'two' => ['two1', 'two3'],

'three'

],

]);

$flattened = $collection->flatten();

dd($flattened);

}

Output:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[0] => Hardik

[1] => admin

[2] => manager

[3] => superadmin

[4] => one

[5] => two1

[6] => two3

[7] => three

)

)

Laravel Collection flatten() with depth Example

public function index()

{

$collection = collect([

'Apple' => [

['name' => 'iPhone 7S', 'brand' => 'Apple'],

],

'Vivo' => [

['name' => 'V-85', 'brand' => 'Vivo']

],

]);

$flattened = $collection->flatten(1);

dd($flattened);

}

Output:

Also see:Laravel Collection Map Method Example

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[0] => Array

(

[name] => iPhone 7S

[brand] => Apple

)

[1] => Array

(

[name] => V-85

[brand] => Vivo

)

)

)

I hope it can help you…

Hope this code and post will helped you for implement Laravel Collection Flatten Method Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

84  +    =  86

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US