toArray method is similar to the
all method in that it will return the underlying array that the collection instance is using. The difference, however, is that the
toArray method will convert any object instance it can into arrays (namely any object that implements the "Illuminate\Contracts\Support\Arrayable" interface).
The signature of the
toArray method is:
public function toArray();
Consider the following code:
use Illuminate\Support\Collection; $items = [ 'first' => 'I am first', 'second' => 'I am second', 'third' => new Collection([ 'first' => 'I am nested' ]) ]; $collection = Collection::make($items); $returnedItems = $collection->toArray();
After the above code has executed, the
$returnedItems variable would contain a value similar to the following output:
array 'first' => string 'I am first' 'second' => string 'I am second' 'third' => array 'first' => string 'I am nested'