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 fill a column automatically while creating records in Laravel
- How to remove P tag from CkEditor in Laravel?
- How to upload multiple images after preview in laravel using cropper js
- How to pass query string with pagination in laravel
- How to use bootstrap pagination in laravel 8
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- In order to use the Auth::routes() method, please install the laravel/ui package
- How to update record after save method in Laravel
- Display success message in laravel
- How to increment column value of table in Laravel
- Setup laravel project with docker
- Shorter syntax for whereHas with call back function in laravel
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- How to get only time from created_at in laravel
- How to get route name on visit URL in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- How to get tomorrow and yesterday date in laravel
- Return redirect laravel not working
- Pass variable from blade to controller Laravel
- How to print form data in laravel
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- Display option of select as selected with blade directive Laravel
- How to send email in laravel
- How to call model in blade laravel
- Laravel 9 pagination with search filter