Use array_replace_recursive() if available.
authorJan Schneider <jan@horde.org>
Mon, 3 Jan 2011 11:56:30 +0000 (12:56 +0100)
committerJan Schneider <jan@horde.org>
Mon, 3 Jan 2011 11:56:30 +0000 (12:56 +0100)
framework/Util/lib/Horde/Array.php
framework/Util/test/Horde/Util/ArrayTest.php

index ecdb753..32aa358 100644 (file)
@@ -213,6 +213,10 @@ class Horde_Array
      */
     static public function array_merge_recursive_overwrite(array $a1, array $a2)
     {
+        if (function_exists('array_replace_recursive')) {
+            return array_replace_recursive($a1, $a2);
+        }
+
         foreach ($a2 as $key => $val) {
             if (!isset($a1[$key])) {
                 $a1[$key] = array();
index 4205d16..3ac74fa 100644 (file)
@@ -1,12 +1,16 @@
 <?php
 /**
+ * Require our basic test case definition
+ */
+require_once dirname(__FILE__) . '/Autoload.php';
+
+/**
  * @author     Jan Schneider <jan@horde.org>
  * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
  * @category   Horde
  * @package    Util
  * @subpackage UnitTests
  */
-
 class Horde_Util_ArrayTest extends PHPUnit_Framework_TestCase
 {
     public function setUp()
@@ -49,4 +53,22 @@ class Horde_Util_ArrayTest extends PHPUnit_Framework_TestCase
             $this->array
         );
     }
+
+    public function testArrayMergeRecursive()
+    {
+        $this->assertEquals(
+            array('one' => 1,
+                  'two' => array('two/one' => 1,
+                                 'two/two' => 22),
+                  'three' => 33,
+                  'four' => 4),
+            Horde_Array::array_merge_recursive_overwrite(
+                array('one' => 1,
+                      'two' => array('two/one' => 1,
+                                     'two/two' => 2),
+                      'three' => 3),
+                array('two' => array('two/two' => 22),
+                      'three' => 33,
+                      'four' => 4)));
+    }
 }