0

Update. this version works now - in case anyone is interested:

/**
 * returns an array merge from two arrays. Keeps associative keys only. Prefers arrays as values. 
 * If a key in both arrays has arrays as values the procedure is continued on the next deeper level.
 * If a key in both arrays has non-array value then the value from array2 will be used.
 * 
 * @example: 
 *     $array1 = array(1, "a" => array("value"), "b" => array("value", "ba" => 5), "c" => "value", "d" => 5);
 *     $array2 = array("a" => "value", "b" => array("bb" => 15), "c" => "otherValue");
 *     $result = array("a" => array("value"), "b" => array("ba" => 5, "bb" => 15), "c" => "otherValue", "d" => 5);
 *   
 * @param array $array1 array to merge
 * @param array $array2 array to merge
 *
 * @return array
 */
public static function arrayMergeDeep($array1, $array2)
{   
    $return = array();

    $keys1 = array_keys($array1);
    $keys2 = array_keys($array2);
    $allKeys = array_unique(array_merge($keys1, $keys2));

    foreach($allKeys as $key) {

        if(is_string($key)) {

            if(in_array($key, $keys1, true) && !in_array($key, $keys2, true)) {

                $return[$key] = $array1[$key];

            } elseif(!in_array($key, $keys1, true) && in_array($key, $keys2, true)) {

                $return[$key] = $array2[$key];

            } elseif(in_array($key, $keys1, true) && in_array($key, $keys2, true)) {

                if (is_array($array1[$key]) && !is_array($array2[$key])) {

                    $return[$key] = $array1[$key];

                } elseif (!is_array($array1[$key])) {

                    $return[$key] = $array2[$key];

                } elseif (is_array($array1[$key]) && is_array($array2[$key])) {

                    $return[$key] = Utils_Array::arrayMergeDeep($array1[$key], $array2[$key]);

                } else {

                    throw new Exception('No way to end up here corectly.');
                }

            } else {

                throw new Exception('No way to end up here corectly.');
            }

        }

    }

    return $return;

}

earlier problem was non strict in_array search. now this works.

the text below is to make the thext above uploadable. the text below is to make the thext above uploadable. the text below is to make the thext above uploadable. the text below is to make the thext above uploadable. the text below is to make the thext above uploadable. the text below is to make the thext above uploadable. the text below is to make the thext above uploadable. the text below is to make the thext above uploadable. the text below is to make the thext above uploadable. the text below is to make the thext above uploadable.

Peter Frey
  • 361
  • 4
  • 17
  • I'd suggest passing your output variable in by reference, so you odn't have to care about where your returns are going - just return true until you stop. http://php.net/manual/en/language.references.pass.php – scrowler Dec 04 '13 at 18:14
  • Hi scrowler, can you help me with how you would do this? I am not getting how this should work. when I simply add &$return in the parameters nothing changes: public static function arrayMergeDeep($array1, $array2, **&$return)** – Peter Frey Dec 06 '13 at 11:23

1 Answers1

0

got the problem. It was not related to anything like recursion. It was simply

in_array($key, $keys1)

needs to become

in_array($key, $keys1, true)

etc. Where "true" is for STRICT. Otherwise there are matches with string keys to 0 index.

Peter Frey
  • 361
  • 4
  • 17