combine() Method | Laravel Collection Example

  • 13-05-2022
  • 1544
  • Laravel 9
  • Haresh Chauhan

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-image

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

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :