t-labs-co/laravel-array-macros 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

t-labs-co/laravel-array-macros

Composer 安装命令:

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

包简介

Laravel Array Macros

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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?

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

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

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

You can install the package via composer:

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

Usage

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

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

<?php
$array = [1, 2, 3];
$result = Arr::lastIf($array, true); // Returns 3

chunk

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]

getAnyValues

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

hasAnyValues

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

hasAllValues

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

ifOk

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]

missing

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']

isMissing

Checks if a key is missing from an array.

<?php
$array = ['a' => 1, 'b' => 2];
$result = Arr::isMissing($array, 'c'); // Returns true

range

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]

renameKeys

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]

swap

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]

validate

Validates all items in an array using a rule or callable.

<?php
$array = [1, 2, 3];
$result = Arr::validate($array, 'integer'); // Returns true

isNumeric

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)

odd

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)

even

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)

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

t-labs-co/laravel-array-macros 适用场景与选型建议

t-labs-co/laravel-array-macros 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「array-macros」 「T.Labs Co.」 「laravel-array-macros」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 t-labs-co/laravel-array-macros 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 t-labs-co/laravel-array-macros 我们能提供哪些服务?
定制开发 / 二次开发

基于 t-labs-co/laravel-array-macros 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-05