0

我的第一个问题不是很清楚,所以我删除了它,这是一个重新表述;我有这些数组:

    $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
...

所以我需要一些帮助来重新制定这个函数以获得预期的结果。

4

1 回答 1

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);

}

它有帮助吗?

于 2013-10-30T22:05:39.623 回答