In this tutorial, We will learn how to work the laravel array prepend() function. We can push the element at the beginning of an array.
Example:
Create any controller file and put the following code to understand prepend() function of laravel.
<?php
namespace App\Http\Controllers;
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);
//['Desk', 'Table', 'Chair','Basket']
$array2 = ['php', 'laravel', 'html', 'css'];
$prepended2 = Arr::prepend($array2, 'Vue');
print_r($prepended1);
//['php', 'laravel', 'html','css','Vue']
}
}
Output:
['Desk', 'Table', 'Chair','Basket']
['php', 'laravel', 'html','css','Vue'] |
Thanks, May this example will help you.