项目作者: rogervila

项目描述 :
Compare the difference between two multidimensional arrays
高级语言: PHP
项目地址: git://github.com/rogervila/array-diff-multidimensional.git
创建时间: 2017-02-20T18:34:17Z
项目社区:https://github.com/rogervila/array-diff-multidimensional

开源协议:

下载


Array Diff Multidimensional

Build Status
StyleCI
Total Downloads
Latest Stable Version
License

Works like the PHP array_diff() function, but with multidimensional arrays.

Install

Via composer:

  1. composer require rogervila/array-diff-multidimensional

Usage

  1. use Rogervila\ArrayDiffMultidimensional;
  2. $new = [
  3. 'a' => 'b',
  4. 'c' => [
  5. 'd' => 'e',
  6. 'f' => 'Hello',
  7. ],
  8. ];
  9. $old = [
  10. 'a' => 'b',
  11. 'c' => [
  12. 'd' => 'e',
  13. 'f' => 'Goodbye',
  14. ],
  15. ];
  16. // Compare the arrays by calling the 'compare' class method
  17. $result = ArrayDiffMultidimensional::compare($new, $old)
  18. // Or by calling the global helper function
  19. $result = array_diff_multidimensional($new, $old)
  20. var_dump($result);

The result of comparing $new with $old will return a new array with the changes:

  1. [
  2. 'c' => [
  3. 'f' => 'Hello'
  4. ],
  5. ]

Strict vs. Loose comparisons

Comparisons are strict by default, but you can specify that you want to make a loose comparison passing a boolean as the third parameter for compare method or calling the looseComparison

  1. // Passing 'false' as a third parameter will deactivate the strict comparison mode
  2. ArrayDiffMultidimensional::compare($new, $old, false);
  3. array_diff_multidimensional($new, $old, false);
  4. // This method call is equivalent
  5. ArrayDiffMultidimensional::looseComparison($new, $old);

Also, a strictComparison method is available for more clarity

  1. // Comparisons are strict by default
  2. ArrayDiffMultidimensional::compare($new, $old);
  3. array_diff_multidimensional($new, $old);
  4. // This method call is equivalent
  5. ArrayDiffMultidimensional::strictComparison($new, $old);

License

Array Diff Multidimensional is an open-sourced package licensed under the MIT license.