rogervila / array-diff-multidimensional
比较两个多维数组之间的差异
2.1.0
2021-10-28 08:07 UTC
Requires (Dev)
- phpunit/phpunit: ^4.8 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0
This package is auto-updated.
Last update: 2024-08-29 07:28:48 UTC
README
它类似于 PHP array_diff() 函数,但支持多维数组。
安装
通过 composer
composer require rogervila/array-diff-multidimensional
使用方法
use Rogervila\ArrayDiffMultidimensional; $new = [ 'a' => 'b', 'c' => [ 'd' => 'e', 'f' => 'Hello', ], ]; $old = [ 'a' => 'b', 'c' => [ 'd' => 'e', 'f' => 'Goodbye', ], ]; // Compare the arrays by calling the 'compare' class method $result = ArrayDiffMultidimensional::compare($new, $old) // Or by calling the global helper function $result = array_diff_multidimensional($new, $old) var_dump($result);
比较 $new
和 $old
的结果将返回一个包含更改的新数组
[ 'c' => [ 'f' => 'Hello' ], ]
严格比较与松散比较
默认情况下比较是严格的,但您可以通过将布尔值作为 compare
方法的第三个参数传递或调用 looseComparison
来指定您想要进行松散比较
// Passing 'false' as a third parameter will deactivate the strict comparison mode ArrayDiffMultidimensional::compare($new, $old, false); array_diff_multidimensional($new, $old, false); // This method call is equivalent ArrayDiffMultidimensional::looseComparison($new, $old);
此外,还有一个 strictComparison
方法,以提供更清晰的比较
// Comparisons are strict by default ArrayDiffMultidimensional::compare($new, $old); array_diff_multidimensional($new, $old); // This method call is equivalent ArrayDiffMultidimensional::strictComparison($new, $old);
许可证
Array Diff Multidimensional 是一个开源软件包,根据 MIT 许可证 发布。