PHPackages                             t-labs-co/laravel-array-macros - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. t-labs-co/laravel-array-macros

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

t-labs-co/laravel-array-macros
==============================

Laravel Array Macros

1.0.0(1y ago)12[4 PRs](https://github.com/T-Labs-Co/laravel-array-macros/pulls)MITPHPPHP ^8.4||^8.3||^8.2CI passing

Since Apr 5Pushed 1mo agoCompare

[ Source](https://github.com/T-Labs-Co/laravel-array-macros)[ Packagist](https://packagist.org/packages/t-labs-co/laravel-array-macros)[ Docs](https://github.com/t-labs-co/laravel-array-macros)[ GitHub Sponsors](https://github.com/ty-huynh)[ RSS](/packages/t-labs-co-laravel-array-macros/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (13)Versions (6)Used By (0)

Laravel Array Macros
====================

[](#laravel-array-macros)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e060b1ae624a074eadebd2c2b5e39864acc33585ff6a2b0b86641f2dca704f93/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f742d6c6162732d636f2f6c61726176656c2d61727261792d6d6163726f732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/t-labs-co/laravel-array-macros)[![GitHub Tests Action Status](https://camo.githubusercontent.com/a03f4be5a6d5939fc5ab670c3e2e40d18efe9b3553ff7dbf6cd4d7ff44c442bf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f742d6c6162732d636f2f6c61726176656c2d61727261792d6d6163726f732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/t-labs-co/laravel-array-macros/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/b9bdec5173608b74209eb57ee48d6da094db5381bc2173fbde7a2abb3afe5088/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f742d6c6162732d636f2f6c61726176656c2d61727261792d6d6163726f732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/t-labs-co/laravel-array-macros/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/e075d9acf9b009b4315617ecc11d4a7bfa5b11a77221aa986750f9e658121486/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f742d6c6162732d636f2f6c61726176656c2d61727261792d6d6163726f732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/t-labs-co/laravel-array-macros)

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.

Why Use This Package?
---------------------

[](#why-use-this-package)

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.

Contact Us
----------

[](#contact-us)

(c) T.Labs &amp; Co. Contact for Work: T.

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.

Features
--------

[](#features)

- `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.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require t-labs-co/laravel-array-macros
```

Usage
-----

[](#usage)

### firstIf

[](#firstif)

Returns the first element of an array if a condition is met.

```
$array = [1, 2, 3];
$result = Arr::firstIf($array, true); // Returns 1
```

### lastIf

[](#lastif)

Returns the last element of an array if a condition is met.

```
