1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
From: =?utf-8?q?David_Pr=C3=A9vot?= <taffit@debian.org>
Date: Sun, 28 Sep 2014 15:40:45 -0400
Subject: Allow to compare arrays recursively
Author: Marius Ghita <mhitza@gmail.com>
Origin: other, https://stackoverflow.com/questions/3876435/recursive-array-diff#3877494
Forwarded: https://github.com/zetacomponents/ConsoleTools/pull/5
---
tests/input_test.php | 34 +++++++++++++++++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
diff --git a/tests/input_test.php b/tests/input_test.php
index e0f58ee..07c84ca 100644
--- a/tests/input_test.php
+++ b/tests/input_test.php
@@ -2870,12 +2870,44 @@ class ezcConsoleInputTest extends ezcTestCase
'ezcConsoleOptionExclusionViolationException'
);
}
+
+ private function arrayRecursiveDiff($aArray1, $aArray2)
+ {
+ $aReturn = array();
+
+ foreach ($aArray1 as $mKey => $mValue)
+ {
+ if (array_key_exists($mKey, $aArray2))
+ {
+ if (is_array($mValue))
+ {
+ $aRecursiveDiff = $this->arrayRecursiveDiff($mValue, $aArray2[$mKey]);
+ if (count($aRecursiveDiff))
+ {
+ $aReturn[$mKey] = $aRecursiveDiff;
+ }
+ }
+ else
+ {
+ if ($mValue != $aArray2[$mKey])
+ {
+ $aReturn[$mKey] = $mValue;
+ }
+ }
+ }
+ else
+ {
+ $aReturn[$mKey] = $mValue;
+ }
+ }
+ return $aReturn;
+ }
private function commonProcessTestSuccess( $args, $res )
{
$this->input->process( $args );
$values = $this->input->getOptionValues();
- $this->assertTrue( count( array_diff( $res, $values ) ) == 0, 'Parameters processed incorrectly.' );
+ $this->assertTrue( count( $this->arrayRecursiveDiff( $res, $values ) ) == 0, 'Parameters processed incorrectly.' );
}
private function commonProcessTestFailure( $args, $exceptionClass, $message = null )
|