How to use flatten() helper function to convert a multi-dimensional array into a single array
Laravel

How to use flatten() helper function to convert a multi-dimensional array into a single array

Hello, In this tutorial, I would like to show you, how you can convert a multi-dimensional array into a single array. Sometimes, we fetch some problems when we fetch multi-relational data. We can use flatter() helper, to easily convert a more readable array. Let me show how to do that,

Example: 

use Illuminate\Support\Arr;

        $array = [
        'name' => 'english',
        'languages' =>
            'programming'=> [
                'php',
                'java',
                'c++',
                'c#',
            ],
        ];

        $flattened = Arr::flatten($array);

Output:

=> [
     "english",
    "php",
    "java",
    "c",
    "c++",
  ]

 

Hope, it will help you.

Get The latest Coding solutions.

Subscribe to the Email Newsletter