Search

Laravel Collection Public API: values

November 30, 2016 —John Koster

values

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.

The following example demonstrates the usage of the values method:

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([
7 5 => 'five',
8 6 => 'six',
9 7 => 'seven'
10]);
11 
12// Get the collection values.
13$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)[134]
2 protected 'items' =>
3 array (size=3)
4 0 => string 'five' (length=4)
5 1 => string 'six' (length=3)
6 2 => string 'seven' (length=5)