2017 © Pedro Peláez
 

library array-diff

Component for calculating difference of arrays

image

aa/array-diff

Component for calculating difference of arrays

  • Sunday, February 5, 2017
  • by AAstakhov
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

ArrayDiff Calculator

SensioLabsInsight, (*1)

ArrayDiff Calculator works similar to a php function array_diff_assoc but calculates a difference of arrays in a better way due to:, (*2)

  • using expressions (value matching),
  • providing extended information about an array difference.

Example, (*3)

$array1 = ['a' => 8, 'b' => 2, 'c' => 3];
$array2 = ['a' => 6, 'b' => 2, 'd' => 3];

$calc = new Calculator(new SimpleMatcher());
$diff = $calc->calculateDiff($array1, $array2);

print $diff->toString();

outputs:, (*4)

missing:
    -
        key_path: c
        expected: 3
unmatched:
    -
        key_path: a
        expected: 8
        actual: 6

because, (*5)

  • Item with the key 'c' is missing in array2
  • Items with key 'a' have different values (unmatched)

Advanced example, (*6)

Let's assume we have two arrays:, (*7)

$array1 = [
    'name'     => '',
    'price'    => ' ',
    'in_stock' => '',
    'isbns'    => [
        'isbn-10' => '',
    ],
    'pages'    => '',
];
$array2 = [
    'name'     => 'The Lord of the Rings',
    'price'    => '25.99 EUR',
    'in_stock' => true,
    'isbns'    => [
        'isbn-10' => '1230260002385',
    ],
    'pages'    => 567,
];

You can calculate a difference of these arrays using an expression matching:, (*8)

$calc = new Calculator(new ExpressionMatcher());
$diff = $calc->calculateDiff($array1, $array2);

print $diff->toString();

It returns:, (*9)

missing: {  }
unmatched:
    -
        key_path: pages
        expected: '<type.datetime>'
        actual: 567

because, (*10)

  • keys of array1 match keys of array2, so there are no missing items
  • string 'The Lord of the Rings' matches the expression <type.string>
  • '25.99 EUR' matches the compound expression <type.float(2)> <type.string>
  • 'true' matches the expression <type.boolean>
  • isbn value '1230260002385' matches the expression <type.string>
  • page count '567' doesn't match the expression <type.datetime>

Expressions have syntax <expresion-name(param1, param2,...)>., (*11)

The Versions

05/02 2017

dev-master

9999999-dev https://github.com/AAstakhov/array-diff

Component for calculating difference of arrays

  Sources   Download

MIT

The Requires

 

The Development Requires