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 :
- How To Connect PostgreSQL With Laravel Application
- Convert base64 String To Image And Upload In Laravel
- Image Upload From Url Example
- Call to undefined function Gregwar\Captcha\imagecreatetruecolor()
- Laravel Routing Multiple Method Use In Single Route
- How to use Date Format Validation in Laravel?
- How To Exchange Currency Rates In Laravel Example
- Laravel Livewire CRUD Using Bootstrap Modal Full Example Guideline And Demo Source Code
- How To Clone Laravel
- could not find driver (SQL: select count(*) as aggregate from "users" where "email" = admin@gmail.com)