In this tutorial you will learn about the Laravel Arr add() function Example and its application with practical example.
Laravel Arr add() function Example
In this article, we will learn about laravel array add() function. In this tutorial we will learn about the Laravel Arr add() method’s syntax and use with example. I’ll show you how to use Laravel Arr add() function with Example.
Table Of Contents−
Laravel Arr::add method
Laravel Arr::add method adds the given $key and $value to an $array if the $key doesn’t already exist within the given $array. If the $key already exists then it will replaces the given $value in a array.
Syntax:-
1 |
public static function add($array,$key,$value); |
How to use Laravel Arr::add method
Here, I will give you full example for simply Arr add() method in laravel as bellow.
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 28 29 30 31 32 33 34 35 36 37 |
<?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 = Arr::add(['name' => 'Steve'], 'Keith', 100); print_r($array1); echo "<br>"; $array2 = Arr::add(['name' => 'Steve', 'Keith' => null], 'Keith', 200); print_r($array2); } } |
Output:-
1 2 |
Array ( [name] => Steve [Keith] => 100 ) Array ( [name] => Steve [Keith] => 200 ) |