What is this library and why is it needed
\n\nSpatie is not just one library, but a whole ecosystem of high-quality PHP packages developed by the Belgian company of the same name. Spatie is known for its approach to creating clean, testable, and well-documented code. Their packages cover a wide range of tasks: from working with media files and query caching to managing roles and permissions.
\n\nThe Spatie Collection Utilities (spatie/collection-utilities) is a set of helper functions and classes that extend the standard capabilities of arrays and collections in PHP. It provides convenient methods for filtering, sorting, transforming, and analyzing data, making code more readable and concise.
\n\nWhy is it needed? In modern PHP development (especially in frameworks like Laravel), working with data collections is a daily task. Spatie Collection Utilities eliminates the need to write repetitive loops and conditions by offering ready-made, well-tested solutions. This speeds up development, reduces the number of errors, and improves code maintainability.
\n\nInstallation
\n\nInstallation of Spatie packages is done via Composer, the standard dependency manager for PHP.
\n\ncomposer require spatie/collection-utilities\n\nIf you are using Laravel, the package is automatically registered thanks to Composer's autoloading. For other frameworks or plain PHP, simply add require 'vendor/autoload.php'; to your script.
\n\nQuick start with code example
\n\nAfter installation, you can immediately start using the utilities. The main class you will work with is Spatie\\CollectionUtilities\\Collection. It extends the standard Illuminate\\Support\\Collection class (from Laravel) or can be used as a standalone class.
<?php\nrequire 'vendor/autoload.php';\n\nuse Spatie\\CollectionUtilities\\Collection;\n\n// Create a collection\n$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);\n\n// Use the filter method to get even numbers\n$evenNumbers = $collection->filter(fn($item) => $item % 2 === 0);\n\n// Use the map method to multiply each element by 2\n$doubled = $evenNumbers->map(fn($item) => $item * 2);\n\n// Use the toArray method to convert back to an array\nprint_r($doubled->toArray());\n// Output: Array ( [0] => 4 [1] => 8 [2] => 12 [3] => 16 [4] => 20 )\n?>\n\nThis simple example demonstrates method chaining, which is a key feature of working with Spatie collections.
\n\nMain methods/functions
\n\n1. filter()
\nWhat it does: Filters the collection using a callback function. Returns only those elements for which the callback returned true.
Parameters:
\n- \n
callback(callable): A function that receives the current element and its key. Must returntrueorfalse. \n mode(int): Optional. Filtering mode (default isARRAY_FILTER_USE_BOTH). \n
Working code example:
\n$users = new Collection([\n ['name' => 'Alice', 'age' => 25],\n ['name' => 'Bob', 'age' => 17],\n ['name' => 'Charlie', 'age' => 30],\n]);\n\n$adults = $users->filter(fn($user) => $user['age'] >= 18);\n// Result: Collection with Alice and Charlie\n\n2. map()
\nWhat it does: Applies a callback function to each element of the collection and returns a new collection with the transformed elements.
\nParameters:
\n- \n
callback(callable): A function that receives the current element and its key. Must return the transformed value. \n
Working code example:
\