You are here

function js_array_replace_recursive in JS Callback Handler 7.2

Wrapper function for array_replace_recursive().

File

./js.module, line 1004
JavaScript callback handler module.

Code

function js_array_replace_recursive($array, $array1) {

  // PHP 5.3+ has this function built in.
  if (function_exists('array_replace_recursive')) {
    return call_user_func_array('array_replace_recursive', func_get_args());
  }

  /**
   * Internal recursion function.
   */
  function recurse($array, $array1) {
    foreach ($array1 as $key => $value) {

      // Create new key in $array, if it is empty or not an array.
      if (!isset($array[$key]) || isset($array[$key]) && !is_array($array[$key])) {
        $array[$key] = array();
      }

      // Overwrite the value in the base array.
      if (is_array($value)) {
        $value = recurse($array[$key], $value);
      }
      $array[$key] = $value;
    }
    return $array;
  }

  // Handle the arguments, merge one by one.
  $args = func_get_args();
  $array = $args[0];
  if (!is_array($array)) {
    return $array;
  }
  for ($i = 1; $i < count($args); $i++) {
    if (is_array($args[$i])) {
      $array = recurse($array, $args[$i]);
    }
  }
  return $array;
}