Php 可以组合阵列吗?

Php 可以组合阵列吗?,php,arrays,Php,Arrays,是否可以合并所有现有数组的内容?如果密钥已在使用中,则为空;如果未使用,则必须从其他数组添加密钥,但必须使用空值 array(3) { [0]=> array(4) { ["attributes_3_ru-ru"] => "10" ["attributes_3_en-gb"] => "100" ["attributes_4_en-gb"] => "2

是否可以合并所有现有数组的内容?如果密钥已在使用中,则为空;如果未使用,则必须从其他数组添加密钥,但必须使用空值

  array(3) {
      [0]=>
            array(4) {
              ["attributes_3_ru-ru"] => "10"
              ["attributes_3_en-gb"] => "100"
              ["attributes_4_en-gb"] => "2222"
              ["attributes_4_ru-ru"] => ""
            }
      [1]=>
            array(2) {
              ["attributes_6_ru-ru"] => "10"
              ["attributes_6_en-gb"] => "100"
            }
        }
      [2]=>
            array(2) {
              ["attributes_4_ru-ru"] => "10"
              ["attributes_4_en-gb"] => "100"
            }
            ...n
    }
输出是这样的

array(3) {
  [0]=>
        array(4) {
          ["attributes_3_ru-ru"] => "10"
          ["attributes_3_en-gb"] => "100"
          ["attributes_4_en-gb"] => "2222"
          ["attributes_4_ru-ru"] => ""
          ["attributes_6_ru-ru"] => ""
          ["attributes_6_en-gb"] => ""
        }
  [1]=>
        array(2) {
          ["attributes_6_ru-ru"] => "10"
          ["attributes_6_en-gb"] => "100"
          ["attributes_3_ru-ru"] => ""
          ["attributes_3_en-gb"] => ""
          ["attributes_4_en-gb"] => ""
          ["attributes_4_ru-ru"] => ""
        }
    }
  [2]=>
        array(2) {
          ["attributes_4_ru-ru"] => "10"
          ["attributes_4_en-gb"] => "100"
          ["attributes_6_ru-ru"] => ""
          ["attributes_6_en-gb"] => ""
          ["attributes_3_ru-ru"] => ""
          ["attributes_3_en-gb"] => ""
        }
        ...n
    }
对此,您应该使用array_merge()

$megaArray = array_merge($array1, $array2, $array3);
或者,如果我过于简化了您的用例,并且出于某种原因您需要将数据附加到$array3,那么您可以使用foreach()和in_array()。符合以下要求的东西(未测试)


我会这样做。首先,收集所有键并创建一个“模板”数组,其中包含空值

$merged = array_merge(...$arrays);
$template = array_fill_keys(array_keys($merged), '');
然后将其映射到原始数组上,并将每个条目与模板合并

$result = array_map(function($entry) use ($template) {
    return array_merge($template, $entry);
}, $arrays);

是的,这是可能的。@u_mulder所以也许你知道谁会意识到这个复制品的可能复制品根本没有解决这个问题。它将多个数组合并到一个数组中。它对它进行寻址,只需在数组上循环并根据需要合并数组即可。数组填充键是什么?何时必须使用它?@whoami数组填充键获取一个数组和一个值,并使用输入数组作为键创建一个新数组,其中每个键都具有相同的单个值。
$result = array_map(function($entry) use ($template) {
    return array_merge($template, $entry);
}, $arrays);