【问题标题】:PHP array merging issuePHP数组合并问题
【发布时间】:2012-07-18 16:19:14
【问题描述】:

我相信过去有很多类似的问题,如果之前出现过,很抱歉。基本上,我试图合并两个多维数组,而不是为任何重复的键创建 2 个键。

这是一个例子:

$one = array(
    'foo' => array(
        'bar' => array(
            'hello' => 'world',
            'boom'  => 'universe'
        ),
        'whiz' => array(
            'wham' => array(
                'blam' => 'kaplow'
            )
        )
    )
);

$two = array(
    'foo' => array(
        'whiz' => 'woo',
        'king' => array(
            'kong' => 'animal'
        )
    )
);

如果我使用array_merge_recursive($one, $two);,我会得到以下结果:

array(1) {
  ["foo"]=>
  array(3) {
    ["bar"]=>
    array(2) {
      ["hello"]=>
      string(5) "world"
      ["boom"]=>
      string(8) "universe"
    }
    ["whiz"]=>
    array(2) {
      ["wham"]=>
      array(1) {
        ["blam"]=>
        string(6) "kaplow"
      }
      // This is the problem.
      [0]=>
      string(3) "woo"
    }
    ["king"]=>
    array(1) {
      ["kong"]=>
      string(6) "animal"
    }
  }
}

如果我使用array_merge($one, $two);,我会得到以下结果:

array(1) {
  ["foo"]=>
  array(2) {
    // This is good but the rest of the array is gone.
    ["whiz"]=>
    string(3) "woo"
    ["king"]=>
    array(1) {
      ["kong"]=>
      string(6) "animal"
    }
  }
}

这是我想要的输出:

array(1) {
  ["foo"]=>
  array(3) {
    ["bar"]=>
    array(2) {
      ["hello"]=>
      string(5) "world"
      ["boom"]=>
      string(8) "universe"
    }
    // Key is replaced, rest of the array remains intact.
    ["whiz"]=>
    string(3) "woo"
    ["king"]=>
    array(1) {
      ["kong"]=>
      string(6) "animal"
    }
  }
}

所以基本上,我追求array_merge_recursive() 的功能,但它也能像array_replace_recursive() 一样工作,你们有什么想法吗?

--

我现在已经接受了一个答案,但不要因为展示任何其他可能更好的方法而气馁,我会回来查看。

谢谢大家。

【问题讨论】:

  • 如果您将您实际期望的内容作为输出包含在内,将会有所帮助。 array_replace_recursive 的行为似乎是对问题最直接的解释。
  • 这不是递归的,但是像$merged = $one['foo'] + $two['foo']; 这样的东西不工作吗?
  • 我更新了一个更好的例子。

标签: php arrays multidimensional-array array-merge


【解决方案1】:

认为您正在寻找:

function farray_merge_recursive() {

    if (func_num_args() < 2) {
        trigger_error(__FUNCTION__ .' needs two or more array arguments', E_USER_WARNING);
        return;
    }
    $arrays = func_get_args();
    $merged = array();

    while ($arrays) {
        $array = array_shift($arrays);
        if (!is_array($array)) {
            trigger_error(__FUNCTION__ .' encountered a non array argument', E_USER_WARNING);
            return;
        }
        if (!$array)
            continue;
        foreach ($array as $key => $value)
            if (is_string($key))
                if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key]))
                    $merged[$key] = call_user_func(__FUNCTION__, $merged[$key], $value);
                else
                    $merged[$key] = $value;
            else
                $merged[] = $value;
    }
    return $merged;
}

我想我是从 PHP 手册中偷来的,因为我不想自己写。

我用它来组合和覆盖我的 MVC 框架的配置数组,效果很好。

【讨论】:

  • 这行得通,我只是想知道是否有更好的解决方案。
  • @Daryl 当我详细研究这个时,这是我发现的最快的一个。我确实做了一个,但它已经消失了,所以我只是粘贴了这个。这个函数可以做优化,但据我所知 PHP 本身没有快速古怪的方法。
猜你喜欢
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
相关资源
最近更新 更多