The Laravel Array Macros
package provides a collection of useful macros for Laravel's Arr helper, extending its functionality to simplify common array operations. These macros are designed to make working with arrays in Laravel more efficient and expressive, reducing boilerplate code and improving readability.
This package is ideal for developers who frequently work with arrays in Laravel and want to simplify their code. It provides a wide range of macros that cover common use cases, making array manipulation more intuitive and concise.
(c) T.Labs & Co. Contact for Work: T. hongty.huynh@gmail.com
Got a PHP or Laravel project? We're your go-to team! We can help you:
- Architect the perfect solution for your specific needs.
- Get cleaner, faster, and more efficient code.
- Boost your app's performance through refactoring and optimization.
- Build your project the right way with Laravel best practices.
- Get expert guidance and support for all things Laravel.
- Ensure high-quality code through thorough reviews.
- Provide leadership for your team and manage your projects effectively.
- Bring in a seasoned Technical Lead.
firstIf
: Returns the first element of an array if a condition is met.lastIf
: Returns the last element of an array if a condition is met.chunk
: Splits an array into chunks and applies a callback to each chunk.getAnyValues
: Retrieves the first matching value from an array based on a list of keys.hasAllValues
: Checks if all specified values exist in an array.hasAnyValues
: Checks if any of the specified values exist in an array.ifOk
: Returns the array if a condition is met, otherwise returns null.isMissing
: Checks if a key is missing from an array.missing
: Returns a list of keys that are missing from an array.range
: Creates a range of numbers with optional steps.renameKeys
: Renames keys in an array based on a mapping.swap
: Swaps the values of two keys in an array.validate
: Validates all items in an array using a rule or callable.isNumeric
: Checks if all items in an array are numeric. Supports strict mode to ensure only integers and floats are considered numeric.odd
: Filters an array to include only odd numbers. Supports optional sorting in ascending, descending, or no order.even
: Filters an array to include only even numbers. Supports optional sorting in ascending, descending, or no order.
You can install the package via composer:
composer require t-labs-co/laravel-array-macros
Returns the first element of an array if a condition is met.
$array = [1, 2, 3];
$result = Arr::firstIf($array, true); // Returns 1
Returns the last element of an array if a condition is met.
<?php
$array = [1, 2, 3];
$result = Arr::lastIf($array, true); // Returns 3
Splits an array into chunks and applies a callback to each chunk.
<?php
$array = [1, 2, 3, 4, 5, 6];
$result = Arr::chunk($array, 2, fn($chunk) => array_sum($chunk)); // Returns [3, 7, 11]
Retrieves the first matching value from an array based on a list of keys.
<?php
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$result = Arr::getAnyValues($array, ['b', 'c']); // Returns 2
Checks if any of the specified values exist in an array.
<?php
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$result = Arr::hasAnyValues($array, [2, 4]); // Returns true
Checks if all specified values exist in an array.
<?php
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$result = Arr::hasAllValues($array, [1, 2]); // Returns true
Returns the array if a condition is met, otherwise returns null.
<?php
$array = [1, 2, 3];
$result = Arr::ifOk($array, true); // Returns [1, 2, 3]
Returns a list of keys that are missing from an array.
<?php
$array = ['a' => 1, 'b' => 2];
$result = Arr::missing($array, ['b', 'c']); // Returns ['c']
Checks if a key is missing from an array.
<?php
$array = ['a' => 1, 'b' => 2];
$result = Arr::isMissing($array, 'c'); // Returns true
Creates a range of numbers with optional steps.
<?php
$result = Arr::range(1, 5); // Returns [1, 2, 3, 4, 5]
$result = Arr::range(1, 10, 2); // Returns [1, 3, 5, 7, 9]
Renames keys in an array based on a mapping.
<?php
$array = ['a' => 1, 'b' => 2];
$result = Arr::renameKeys($array, ['a' => 'x', 'b' => 'y']); // Returns ['x' => 1, 'y' => 2]
Swaps the values of two keys in an array.
<?php
$array = ['a' => 1, 'b' => 2];
$result = Arr::swap($array, 'a', 'b'); // Returns ['a' => 2, 'b' => 1]
Validates all items in an array using a rule or callable.
<?php
$array = [1, 2, 3];
$result = Arr::validate($array, 'integer'); // Returns true
Checks if all items in an array are numeric. Supports strict mode to ensure only integers and floats are considered numeric.
<?php
$array = [1, '2', 3.5];
$result = Arr::isNumeric($array); // Returns true
$result = Arr::isNumeric($array, true); // Returns false (strict mode)
Filters an array to include only odd numbers. Supports optional sorting in ascending, descending, or no order.
<?php
$array = [1, 2, 3, 4, 5];
$result = Arr::odd($array); // Returns [1, 3, 5] (ascending order)
$result = Arr::odd($array, -1); // Returns [5, 3, 1] (descending order)
$result = Arr::odd($array, 0); // Returns [1, 3, 5] (no sorting)
Filters an array to include only even numbers. Supports optional sorting in ascending, descending, or no order.
<?php
$array = [1, 2, 3, 4, 5];
$result = Arr::even($array); // Returns [2, 4] (ascending order)
$result = Arr::even($array, -1); // Returns [4, 2] (descending order)
$result = Arr::even($array, 0); // Returns [2, 4] (no sorting)
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.