Convert multidimensional array to single array in Laravel
Convert multidimensional array to single array in Laravel
Through the use of the programming language, we will work together to solve the "Convert multidimensional array to single array in Laravel" puzzle in this lesson.
You can convert multidimensional array to single array in Laravel.-
Convert multidimensional array into single array
//routes\web.php use App\Http\Controllers\HomeController; Route::get('/convert-array',[HomeController::class, 'multidimensionalToSingleArray']); //app\Http\Controllers\HomeController.php public function multidimensionalToSingleArray(){ $myarray = array( array("Ankit", "Ram", "Shyam"), array("Unnao", "Trichy", "Kanpur") ); $arrayData = $this->array_flatten($myarray); print_r($arrayData); } private function array_flatten($array) { if (!is_array($array)) { return FALSE; } $result = array(); foreach ($array as $key => $value) { if (is_array($value)) { $result = array_merge($result, $this->array_flatten($value)); } else { $result[$key] = $value; } } return $result; }
Output :
Array ( [0] => Ankit [1] => Ram [2] => Shyam [3] => Unnao [4] => Trichy [5] => Kanpur )
Input dataarray(
array("Ankit", "Ram", "Shyam"),
array("Unnao", "Trichy", "Kanpur")
);
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to pass query string with pagination in laravel
- How to pass data to multiple partial view files in laravel
- In order to use the Auth::routes() method, please install the laravel/ui package
- Laravel onclick function not working
- Create user in Laravel using tinker
- How to get database name in Laravel 9 ?
- How to call Laravel route in jQuery
- Laravel 5.4 save data to database
- Database transactions in laravel
- Credit card validation in laravel
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Laravel 9 pagination with search filter
- InRandomOrder() method with example in laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- If condition in foreach loop in laravel
- Method chaining in Laravel
- How to add columns in existing table using migration in laravel
- How to check records exist in loaded relationship in Laravel blade view
- Get domain name in laravel
- Laravel recursive function in controller
- Undefined property: stdClass::$title
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Composer create project laravel/laravel example app
- How to create controller in laravel
- How to add foreign key in laravel using migration