Convert Array To Collection in Laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Converting Arrays to Collections in Laravel for Efficient Sorting Body:

In this comprehensive guide, we will delve into the process of converting PHP arrays to collections in Laravel for efficient sorting by desired keys (either 'website' or 'url'). Firstly, let us understand what an array is and why it differs from a collection. Then, we'll explore how to use collections in Laravel and demonstrate appropriate code examples for the conversion process that enables effective sorting.

Arrays vs Collections

An array in PHP is a data structure that stores values indexed by numeric keys starting from 0. Arrays can have mixed elements, allowing you to store various types of data such as strings, integers, objects, and arrays within other arrays. On the other hand, collections are objects that provide additional functionality when dealing with complex data structures.

  • Arrays are simple data containers while collections offer better organization and manipulation capabilities.
  • Collections are immutable by default, meaning they cannot be modified directly. To alter a collection, you must create a new instance or use methods to manipulate the original collection.

Converting Arrays to Collections in Laravel

To convert your PHP array into a Laravel collection, first instantiate the array as a collection using the static collect() method. This will create an instance of Collection from the given value or object.

$myArray = collect(websites);
However, your issue lies in that you are not explicitly sorting your collection by a specific key. The sort() method in Laravel collections sorts the items according to their keys as integers by default (0, 1, 2, etc.). To achieve your desired output and sort the collection by specified keys ('website' or 'url'), you can customize the array before converting it.

Sorting a Collection

Step 1: Create an associative array with the desired key-value pairs, where the value is the original element from your PHP array.

$sortedArray = [];
foreach ($myArray as $item) {
    // Add elements to sortedArray based on a specific key (e.g., 'website' or 'url')
}
  • In this case, your example array would look like:
  • $sortedArray = [
      "example" => "example",
      "example" => "example"
    ];
    

Step 2: Convert your sorted array into a collection with the appropriate key as you desire (either 'website' or 'url').

$sortedCollection = collect($sortedArray);
  • In this case, the final collection could be:
  • {
      "example": "example",
      "example": "example"
    }
    

Conclusion

By understanding the differences between PHP arrays and Laravel collections, you can effectively convert your array to a collection for efficient sorting. In this guide, we have elaborated on the conversion process from arrays to collections while sorting by specified keys. By incorporating relevant code examples, you should now be better equipped to manage your data in Laravel applications.

Note: Remember that sorting is a complex concept with various algorithms and approaches. For more advanced sorting techniques or scenarios that involve multiple criteria, it may be beneficial to consult additional resources, such as our website Advanced Array Sorting in PHP with Collection.