Skip to content

Instantly share code, notes, and snippets.

@heathdutton
Created July 27, 2018 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathdutton/e4fe90927794a17ae29c2caf8e5667c1 to your computer and use it in GitHub Desktop.
Save heathdutton/e4fe90927794a17ae29c2caf8e5667c1 to your computer and use it in GitHub Desktop.
<?php
$rows = [
'row1' => [
'key' => 'value',
'key2' => 'value2',
'key3' => 'value3',
],
'row2' => [
'key' => 'value',
'key2 different' => 'value2',
'key3' => 'value3',
],
'row3' => [
'key' => 'value',
'key2' => 'value2',
'key3' => 'value3',
],
'row4' => [
'key' => 'value',
'key2' => 'value2',
'key3' => 'value3',
],
'row5' => [
'key' => 'value',
'key2' => 'value altered',
'key3' => 'value3',
],
'row6' => [
'key' => 'value',
],
];
function compareAll($rows)
{
// Flatten all possible values for all points in the array
$diff = [];
foreach ($rows as $keya => $rowa) {
$diff[$keya] = [];
foreach ($rows as $keyb => $rowb) {
if ($keyb !== $keya) {
$diff[$keya] = array_merge($diff[$keya], compareOne($rowa, $rowb));
}
}
}
return $diff;
}
function compareOne($rowa, $rowb)
{
ksort($rowa);
ksort($rowb);
if ($rowa === $rowb) {
return [];
}
$diff = [];
foreach ($rowa as $key => $value) {
if (isset($rowb[$key])) {
if (is_array($value)) {
$diff[$key] = compareOne($value, $rowb[$key]);
} else {
if ($value !== $rowb[$key]) {
$diff[$key] = $value;
}
}
} else {
$diff[$key] = $value;
}
}
return $diff;
}
die(var_dump(compareAll($rows)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment