Search

Laravel 5 Collections: Retrieving Only the Values of a Collection's Elements With values

April 22, 2018 —John Koster

The values method can be used to retrieve a new Collection instance containing only the values of the original collection instance. The keys of the new collection will be reset to consecutive numerical keys, starting at zero.

The behavior of the values method is similar to PHP's array_values function.

#Signature

1public function values();

#Example Use

The following example demonstrates the usage of the values method:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 5 => 'five',
6 6 => 'six',
7 7 => 'seven'
8]);
9 
10// Get the collection values.
11$values = $collection->values();

After the above code has executed, the $values variable will be an instance of Collection and contain a value similar to the following output. Take note in how the keys of the associated values have changed (they have been reset starting at 0):

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => string 'five'
5 1 => string 'six'
6 2 => string 'seven'

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.