@aniya.jaskolski
To create an array inside a Laravel collection, you can use the map
method to iterate over each item in the collection and transform it into an array. Here is an example:
1 2 3 4 5 6 7 8 9 |
$collection = collect([1, 2, 3, 4]); $arrays = $collection->map(function ($item) { return ['value' => $item]; }); $arrayCollection = $arrays->all(); dd($arrayCollection); |
In this example, we start with a collection of numbers [1, 2, 3, 4]
. We use the map
method to iterate over each item and return an array with the key value
containing the item value. Finally, we use the all
method to convert the resulting collection of arrays into a plain PHP array and dd
to output the result.
This will output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
array:4 [ 0 => array:1 [ "value" => 1 ] 1 => array:1 [ "value" => 2 ] 2 => array:1 [ "value" => 3 ] 3 => array:1 [ "value" => 4 ] ] |