In this tutorial you will learn about the Laravel Arr prepend() function Example and its application with practical example.
Laravel Arr prepend() function Example
In this article, I’ll show you how to use laravel Arr prepend() function with example. We will show example of Arr prepend() function in laravel. In this tutorial, we will use Arr prepend() function to push an item onto the beginning of an array.
Laravel Arr::prepend method
The Arr::prepend method will push an item onto the beginning of an array.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\FileController; use Illuminate\Support\Arr; class HomeController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { $array1 = ['Desk', 'Table', 'Chair']; $prepended1 = Arr::prepend($array1,'Basket'); print_r($prepended1); echo "<br>"; $array2 = ['php', 'laravel', 'html','css']; $prepended2 = Arr::prepend($array2,'Vue'); print_r($prepended2); } } |
Output:-
1 2 |
Array ( [0] => Basket [1] => Desk [2] => Table [3] => Chair ) Array ( [0] => Vue [1] => php [2] => laravel [3] => html [4] => css ) |