Laravel 5 Collections: Convert a Collection to a Native PHP Array With toArray

1 min read

The 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();

Example Use

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:

  'first'  => string 'I am first'
  'second' => string 'I am second'
  'third'  => 
      'first' => string 'I am nested'


There are no comments. Be the first to comment!

Up next