combine() method combines the values of the collection as key with the value of another array of collection, also you can say that combine one array key with other array values, using the laravel collection combine method you can easy to merge array value with the key to merge the value with other PHP array value.
Sometimes developers need to combine() the one array value with another array key that the time array collection method combines use.
Also you can say that array merge, two or multiple arrays store into single array that time need to combine collection method.
Here below working example that will help you to understand how to use combine method in laravel collection.
Please refer following working example that will help you to understand the use of combined array collection in laravel.
Example 1: Use simple array
$data = [ 'key-1', 'key-2', 'key-3', ]; $data1 = [ 'value-1', 'value-2', 'value-3', ]; $collection = collect($data)->combine($data1); // OUTPUT Illuminate\Support\Collection {#6768 ▼ #items: array:3 [▼ "key-1" => "value-1" "key-2" => "value-2" "key-3" => "value-3" ] }
EXAMPLE 2: Use with array first() method
$data = [ 'key-1', 'key-2', 'key-3', ]; $data1 = [ 'value-1', 'value-2', 'value-3', ]; $collection = collect($data)->combine($data1)->first(); // OUTPUT // "value-1"
EXAMPLE 3: Use with toArray() method
$data = [ 'key-1', 'key-2', 'key-3', ]; $data1 = [ 'value-1', 'value-2', 'value-3', ]; $collection = collect($data)->combine($data1)->toArray(); // OUTPUT array:3 [▼ "key-1" => "value-1" "key-2" => "value-2" "key-3" => "value-3" ]
EXAMPLE 4: Use with Multidimensional array
$data = [ 'key-1', 'key-2', 'key-3', ]; $data1 = [ ['value-1' => 123,'value-4' => 456], ['value-2' => 852,'value-5' => [1,2,3,4,56]], 'value-3', ]; $collection = collect($data)->combine($data1); // OUTPUT Illuminate\Support\Collection {#6768 ▼ #items: array:3 [▼ "key-1" => array:2 [▼ "value-1" => 123 "value-4" => 456 ] "key-2" => array:2 [▼ "value-2" => 852 "value-5" => array:5 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 4 => 56 ] ] "key-3" => "value-3" ] }
By understand above example to use combine method in laravel collection.
We always thanks to you for reading our blogs.
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 Chauhan
(Co-Founder)We Are Also Recommending You :
- All Guides About Soft Delete Laravel 8 And 9 With Examples
- Error : Trying To Get Property Of ID Non-Object All Possible Solution Guideline In Laravel - PHP
- How To Exchange Currency Rates In Laravel Example
- Force Redirect to www From Non-www Using htaccess In Laravel
- [Fixed] Binary Does Not Exist Laravel
- Arr::accessible() | Laravel Helper Function
- Laravel 10 Create Custom Helper Function Example
- Pay10 Payment Gateway Integration In Laravel 10
- Unique violation: 7 ERROR: duplicate key value violates unique constraint "users_pkey" DETAIL: Key (id)=(1) already exists.
- Livewire Create Multiple Select2 Dynamic Dropdown In Laravel 9 8 Example