In this post we will learn how to add key and it's value in array using laravel function, Laravel providing helper function Arr::add() this array helper function will help you to add key value in array.
The Arr::add() helper function adds a key and value pair to an array if the added key or given key does not exist in the array or is set to null value.
So let's take an example for understanding.
<?php use Illuminate\Support\Facades\Route; use Arr; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/arr-add', function () { // Example 1 $array = ['name' => 'webappfix']; // This is an array Arr::add($array,'url','https://webappfix.com.'); // Output => ['name' => 'webappfix','url' => 'https://webappfix.com.']; // Example 2 $array = ['name' => 'webappfix','url' => null]; // This is an array with url null value Arr::add($array,'url','https://webappfix.com.'); // Output => ['name' => 'webappfix','url' => https://webappfix.com.]; });
Above two example will help you to add a key in youe existing array.
We always thanks to you for reading our blogs.
![dharmesh-image](/storage/site-setting/w4q23X3ihxZ9MQ5o4GeFthb0mqCcg4LgG8sIqigI.png)
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
![haresh-image](/storage/site-setting/uESEBDJrsDNffCSc0uNtilcThH7Y0df7oI93WCPP.png)
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Casting Attribute in Laravel Eloquent Models
- Laravel Remove index.php From URL
- How To Add Google reCAPTCHA v3 In HTML Form Based PHP/Laravel Website
- Livewire File Upload Laravel
- Bootstrap 5 Modal Not Working Blade File Laravel
- Looping Variable Laravel Blade File - $loop
- How to Install Laravel 10 With Video Tutorial
- Laravel 10 PDF Export Example With DomPDF
- Laravel 6 - Preview and Crop Image Before Upload using Ajax
- Trying to access array offset on value of type int