xpaw/compare-arrays
Composer 安装命令:
composer require xpaw/compare-arrays
包简介
Diffing multi dimensional arrays the easy way
README 文档
README
A PHP library for comparing multi-dimensional arrays and detecting differences.
Features
- Deep comparison of multi-dimensional arrays
- Detects added, removed, and modified values
- Maintains array structure in the result
- Special handling for float comparison with epsilon
- Flatten results into a single-dimensional array with path keys
Installation
Install via Composer:
composer require xpaw/compare-arrays
Usage
Basic Comparison
use xPaw\CompareArrays\CompareArrays; $oldArray = [ 'user' => [ 'name' => 'John', 'age' => 30, 'settings' => [ 'darkMode' => true, 'notifications' => true ] ] ]; $newArray = [ 'user' => [ 'name' => 'John Doe', 'age' => 30, 'settings' => [ 'darkMode' => true, 'notifications' => false ], 'lastLogin' => '2025-03-20' ] ]; $differences = CompareArrays::Diff($oldArray, $newArray); print_r($differences);
Result:
Array(
[user] => Array(
[name] => xPaw\CompareArrays\ComparedValue Object(
[OldValue] => John
[NewValue] => John Doe
[Type] => modified
)
[settings] => Array(
[notifications] => xPaw\CompareArrays\ComparedValue Object(
[OldValue] => 1
[NewValue] =>
[Type] => modified
)
)
[lastLogin] => xPaw\CompareArrays\ComparedValue Object(
[OldValue] =>
[NewValue] => 2025-03-20
[Type] => added
)
)
)
Flattening Results
To simplify handling of nested differences, you can flatten the result:
$flattened = CompareArrays::Flatten($differences); print_r($flattened);
Result:
Array(
[user/name] => xPaw\CompareArrays\ComparedValue Object(
[OldValue] => John
[NewValue] => John Doe
[Type] => modified
)
[user/settings/notifications] => xPaw\CompareArrays\ComparedValue Object(
[OldValue] => 1
[NewValue] =>
[Type] => modified
)
[user/lastLogin] => xPaw\CompareArrays\ComparedValue Object(
[OldValue] =>
[NewValue] => 2025-03-20
[Type] => added
)
)
Custom Separator and Path Prefix
You can customize the separator and add a path prefix when flattening:
// Using a dot as separator $flattened = CompareArrays::Flatten($differences, '.');
Result:
Array
(
[user.name] => xPaw\CompareArrays\ComparedValue Object(...)
[user.settings.notifications] => xPaw\CompareArrays\ComparedValue Object(...)
[user.lastLogin] => xPaw\CompareArrays\ComparedValue Object(...)
)
API Reference
CompareArrays::Diff(array $Old, array $New): array
Compares two arrays and produces a new array of changes between these arrays.
- The result maintains the same structure as the input arrays
- The deepest values are
ComparedValueobjects - Float values are compared with
PHP_FLOAT_EPSILONfor precision - Special handling for arrays vs non-arrays in corresponding positions
CompareArrays::Flatten(array $Input, string $Separator = '/', ?string $Path = null): array
Flattens a multi-dimensional array into a one-dimensional array.
- Keys are transformed into paths separated by the specified separator
- Optionally prepend a path prefix to all keys
ComparedValue
Each detected difference is represented by a ComparedValue object with:
$Type: One ofadded,removed, ormodified$OldValue: The value from the old array (null for added items)$NewValue: The value from the new array (null for removed items)
Special Cases
Float Comparison
Floating-point values are compared with PHP_FLOAT_EPSILON to handle precision issues:
$differences = CompareArrays::Diff( ['value' => 0.1], ['value' => 0.1 + 0.00000001] ); // Result: empty array (no differences) $differences = CompareArrays::Diff( ['value' => 0.1], ['value' => 0.1 + 0.0001] ); // Result: detects difference
License
This library is licensed under the MIT License.
xpaw/compare-arrays 适用场景与选型建议
xpaw/compare-arrays 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.54k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2019 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「diff」 「compare」 「multi dimensional」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xpaw/compare-arrays 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xpaw/compare-arrays 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xpaw/compare-arrays 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.
A set of useful PHP classes.
PHP Semantic Versioning Parser and Comparator
Schema Diff: Show difference between MySQL databases
Small class for checking ip addresses
统计信息
- 总下载量: 1.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-15