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 :
- Casting Attribute in Laravel Eloquent Models
- How To Calculate Distance Between Two Latitude And Longitude In Laravel
- How to Generate PDF File Using DomPDF In Laravel 9 Example
- REST API Authentication With Laravel Passport - Webappfix
- How To Add Google reCAPTCHA v3 In HTML Form Based PHP/Laravel Website
- Arr::forget() | Laravel Helper Function
- average() Method | Laravel Collection Example
- Laravel 10 Firebase Web Push Notification Tutorial
- How to Send Mail using PHPMailer in Laravel?
- [Fixed] 429: Too Many Attempts When Call API In Laravel 8 9