2017-02-28 130 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