2013-10-30 110 views
0

我的第一個問題並不那麼清楚,所以我刪除它,這裏是一個重新定義; 我有兩個數組:基於一個公共密鑰合併許多數組php

$open = array(array("FAI1","34"),array("FAI2","34"),array("FAI3","34")); 
    $click = array(array("FAI2","52"),array("FAI1","68"),array("FAI3","99")); 
    $unsubscribe = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102")); 
    $def_sent = array(array("FAI1","34",24),array("FAI2","34",23),array("FAI3","34",27)); 
    $SB = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102")); 
    $HB = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102")); 

我搜索了一個函數來合併它們,並得到這樣的結果:

 $result = array(array("FAI1",34,68,102,34,24,102,102) 
    ,array("FAI2","34",23.....), 
    array("FAI3","34",27....)); 

而要做到這一點,我使用的功能,PHP的在線文檔中,這就是功能但

function array_merge_recursive() { 

    $arrays = func_get_args(); 
    $base = array_shift($arrays); 

    foreach ($arrays as $array) { 
     reset($base); 
     while (list($key, $value) = @each($array)) { 
      if (is_array($value) && @is_array($base[$key])) { 
       $base[$key] = array_merge_recursive($base[$key], $value); 
      } else { 
       $base[$key] = $value; 
      } 
     } 
    } 

    return $base; 
} 

不是得到上述結果我得到這個:

FAI1|34 
FAI2|34 
FAI3|34 
FAI2|52 
FAI1|68 
FAI3|99 
... 

所以我需要一些幫助來重新配置這個函數來獲得預期的結果。

回答

1

試試這個功能:

function array_merge_rec() { 
    $arrays = func_get_args(); 
    $result = array(); 
    foreach ($arrays as $arg) { 
     if (is_array($arg)) { 
      foreach ($arg as $item) { 
       if (!isset($result[$item[0]])) { 
        $result[$item[0]] = $item; 
       } else { 
        $result[$item[0]][] = $item[1]; 
       } 
      } 
     } else { 
      echo "$arg skippend because it isn't array\n"; 
     } 
    } 

    return array_values($result); 

}

幫助?

+0

是的,它可以幫助很多謝謝! – Ahmadhc