2017-02-28 143 views
0

我使用此函数的两个合并递归数组:合并数组递归PHP

function array_merge_recursive_distinct(array &$array1, array &$array2) { 

    $merged = $array1; 

    foreach($array2 as $key => &$value) { 

     if(is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { 

      $merged[$key] = array_merge_recursive_distinct($merged[$key], $value); 

     } else { 

      $merged[$key] = $value; 
     } 
    } 

    return $merged; 
} 

对于使用此功能我做下列步骤:

  • 声明一个空数组$ outArray =阵列( );
  • 做一个while循环,我收集我需要
    • 在while循环我打电话array_merge_recursive_distinct功能,以填补空数组递归

的信息。但是最终的阵列中只包含它在最后一次循环期间收集的最后信息。我试图找到一个解决方案,但直到现在我还没有成功。我究竟做错了什么? 递归函数在while循环中取得所有信息(我在递归函数中输入了数组),但好像它一遍又一遍覆盖合并数组。

感谢

回答

0

CakePHP有一个叫Hash不错的类,它实现了一个名为merge()方法谁做的正是你需要的

/** 
* This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`. 
* 
* The difference between this method and the built-in ones, is that if an array key contains another array, then 
* Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for 
* keys that contain scalar values (unlike `array_merge_recursive`). 
* 
* Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays. 
* 
* @param array $data Array to be merged 
* @param mixed $merge Array to merge with. The argument and all trailing arguments will be array cast when merged 
* @return array Merged array 
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::merge 
*/ 
function merge_arrays_recursivelly(array $data, $merge) { // I changed the function name from merge to merge_arrays_recursivelly 
    $args = array_slice(func_get_args(), 1); 
    $return = $data; 

    foreach ($args as &$curArg) { 
     $stack[] = array((array) $curArg, &$return); 
    } 
    unset($curArg); 

    while (!empty($stack)) { 
     foreach ($stack as $curKey => &$curMerge) { 
      foreach ($curMerge[0] as $key => &$val) { 
       if (!empty($curMerge[1][$key]) && (array) $curMerge[1][$key] === $curMerge[1][$key] && (array) $val === $val) { 
        $stack[] = array(&$val, &$curMerge[1][$key]); 
       } elseif ((int) $key === $key && isset($curMerge[1][$key])) { 
        $curMerge[1][] = $val; 
       } else { 
        $curMerge[1][$key] = $val; 
       } 
      } 
      unset($stack[$curKey]); 
     } 
     unset($curMerge); 
    } 
    return $return; 
} 

https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::merge

Hash::merge source code

0

也许像这样的东西可能会得心应手。

function array_merge_recursive_distinct(array &$array1, array &$array2) { 
    $merged = array_merge($array1,$array2); 
    asort($merged); 
    $merged = array_values(array_unique($merged)); 
    return $merged; 
} 
$array1 = []; 
$array2 = [1,2,3,4,5]; 
print_r(array_merge_recursive_distinct($array1,$array2)); 
$array1 = [1,2,3,6,12,19]; 
$array2 = [1,2,3,4,5]; 
print_r(array_merge_recursive_distinct($array1,$array2)); 
// output 
Array 
(
    [0] => 1 
    [1] => 2 
    [2] => 3 
    [3] => 4 
    [4] => 5 
) 
Array 
(
    [0] => 1 
    [1] => 2 
    [2] => 3 
    [3] => 4 
    [4] => 5 
    [5] => 6 
    [6] => 12 
    [7] => 19 
) 

测试它PHP Sandbox