How to index my laravel collection by a property

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Collection Indexing in Laravel: How to Re-index After Sorting

As developers working with data manipulation in frameworks like Laravel, we frequently encounter scenarios where sorting collections results in seemingly chaotic or incorrect indexing. This often happens because standard array/collection keys are preserved during sorting operations, leading to confusion about the actual sequence of the data.

This post dives deep into a common problem: how to correctly re-index a Laravel collection after sorting it by a specific property, ensuring you have clean, sequential numerical indices ready for iteration or database operations.

The Problem: Misaligned Keys After Sorting

Let's examine the scenario you described. You start with a collection where the keys are not strictly sequential integers:

{
"0":{"id":2,"name":"1","days":["2017-04-06","2017-04-07"]},
"1":{"id":3,"name":"2","days":["2017-04-08","2017-04-09"]},
"2":{"id":4,"name":"3","days":["2017-04-10","2017-04-11"]},
"6":{"id":10,"name":"4","days":["2017-04-12","2017-04-13"]},
"3":{"id":5,"name":"5","days":["2017-04-14","2017-04-15"]},
"4":{"id":6,"name":"6","days":["2017-04-16","2017-04-17"]},
"5":{"id":7,"name":"7","days":["2017-04-18","2017-04-19"]}
}

When you sort this collection by the name property, the order of the values changes, but the original associative keys (the indices) remain fixed: 0, 1, 2, 6, 3, 4, 5. This is because sorting algorithms primarily reorder the elements based on their values while preserving the associated keys.

Attempting to use methods like $collection->values() only extracts the values into a new sequential array, which loses the original key-value association entirely, resulting in a simple flat list rather than a properly indexed collection structure.

The Solution: Sorting and Re-indexing for Clean Data

To achieve the desired outcome—a collection sorted by name with contiguous integer keys starting from 0—we need a two-step process: sort first, then re-index. This technique is fundamental when working with data structures in Laravel, whether you are dealing with Eloquent models or raw collections.

The key is to use the result of the sort operation to generate a new, clean index. We can achieve this by sorting the collection and then applying a standard sequential index generator.

Here is a practical approach using PHP's array_values() function after sorting:

use Illuminate\Support\Collection;

// Assume $data is your initial collection (as an array or Collection)
$data = [
    ["id" => 2, "name" => "1", "days" => ["2017-04-06", "2017-04-07"]],
    ["id" => 3, "name" => "2", "days" => ["2017-04-08", "2017-04-09"]],
    ["id" => 4, "name" => "3", "days" => ["2017-04-10", "2017-04-11"]],
    ["id" => 10, "name" => "4", "days" => ["2017-04-12", "2017-04-13"]],
    ["id" => 5, "name" => "5", "days" => ["2017-04-14", "2017-04-15"]],
    ["id" => 6, "name" => "6", "days" => ["2017-04-16", "2017-04-17"]],
    ["id" => 7, "name" => "7", "days" => ["2017-04-18", "2017-04-19"]]
];

// Step 1: Sort the collection by the 'name' property
usort($data, function ($a, $b) {
    return strcmp($a['name'], $b['name']);
});

// Step 2: Re-index the sorted array numerically
$sorted_and_indexed = array_values($data);

print_r($sorted_and_indexed);

Output Example

After executing the sorting and re-indexing steps, you will get the desired sequential order:

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => 1
            [days] => Array
                (
                    [0] => 2017-04-06
                    [1] => 2017-04-07
                )

        )

    [1] => Array
        (
            [id] => 3
            [name] => 2
            [days] => Array
                (
                    [0] => 2017-04-08
                    [1] => 2017-04-09
                )

    // ... and so on, in correct order
)

Best Practices for Collection Management

When managing complex relationships or data sets in Laravel, it is crucial to maintain data integrity. If you are working with Eloquent models, remember that sorting associations or results should always be performed before attempting to assign a new index if you intend to use that sort order as the primary sequence. For more advanced collection manipulation and data structuring, exploring the robust features provided by the Laravel framework can significantly streamline your development process. For deeper insights into building resilient applications using Laravel components, I highly recommend exploring resources from laravelcompany.com.

Conclusion

The confusion surrounding array indexing after sorting is a common pitfall in PHP and object-oriented programming. By separating the act of sorting (reordering the data) from the act of indexing (assigning sequential keys), you gain complete control over your data structure. Always remember to sort the data first, and then use functions like array_values() to ensure your resulting collection is clean, logically ordered, and numerically indexed.