K
K
kolyafat2016-03-27 15:00:41
Laravel
kolyafat, 2016-03-27 15:00:41

Combining arrays into a collection?

Good afternoon, Dear community, I need your help, I can’t figure out the collections, in general the question is the following, there are several key => value arrays, the key is the same for everyone. How to collect all this into one collection in order to write it to the database. All data is brought to the desired form through the map function


$item_name= collect($nm)->map(function($item, $key){
return [
'c_id_item' => $key,
'name_item' => $item
];
})->toArray();
$gr_name= collect($gr)->map(function($item, $key) {
return [
'c_id_item' => $key,
'c_id_group_item' => $item
];
})->toArray();
$all_data= collect([]);
$all_data2= collect([]);
$all_data2= $all_data2->merge([$gr_name]);
$all_data2= $all_data2->merge([$item_name]);
$all_data= $all_data->merge($all_data2->toArray());

Instead of merging as in the manual

$collection = collect(['product_id' => 1, 'name' => 'Desk']); $merged = $collection->merge(['price' => 100, 'discount' => false]); $merged->all(); // ['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]

I get a collection in which $gr_name values ​​first, then $item_name in the tail Tell me
how to combine the data to get ['c_id_item' => hfghfgh, 'c_id_group_item' => dfghfgh, 'name_item' => dfgdfg ], and c_id_item in all arrays are the same. Thanks in advance for the help, two days already nothing happens.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pochepko, 2016-03-30
@DJZT

$collection->push()
Laravel API Collection Push

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question