如何将数组与标识符合并?

时间:2017-04-05 08:37:01

标签: php arrays function

我有两个这样的数组:

$arr1 = ['yellow' => 1, 'red', => 5];
$arr2 = ['gray' => 3, 'black' => 8, 'orange' => 6, 'yellow' => 9];

我想要这个结果:

$merged = ['yellow' => [1, 'arr1'], 'red', => [5, 'arr1'], 'gray' => [3, 'arr2'], 'black' => [8, 'arr2'], 'orange' => [6, 'arr2'], 'yellow' =>  [9, 'arr2']];

但是当我阅读有关array_merge()的文档时,它没有这样的选项。有什么功能可以帮我吗?

3 个答案:

答案 0 :(得分:2)

这可能就是你要找的东西:

<?php
$input = [
    'arr1' => ['yellow' => 1, 'red' => 5],
    'arr2' => ['yellow' => 10, 'gray' => 3, 'black' => 8, 'orange' => 6]
];
$output = [];
array_walk($input, function ($entry, $source) use (&$output) {
    foreach ($entry as $key=>$val) {
        $output[$key][] = [$val, $source];
    }
});
print_r($output);

上述代码的输出显然是:

Array
(
    [yellow] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => arr1
                )

            [1] => Array
                (
                    [0] => 10
                    [1] => arr2
                )

        )

    [red] => Array
        (
            [0] => Array
                (
                    [0] => 5
                    [1] => arr1
                )

        )

    [gray] => Array
        (
            [0] => Array
                (
                    [0] => 3
                    [1] => arr2
                )

        )

    [black] => Array
        (
            [0] => Array
                (
                    [0] => 8
                    [1] => arr2
                )

        )

    [orange] => Array
        (
            [0] => Array
                (
                    [0] => 6
                    [1] => arr2
                )

        )

)

答案 1 :(得分:0)

好的,我花了一点时间才得出这个答案,因为你的问题中有一个错误。请参阅&#39;,&#39;背后的红色&#39;在数组1中。

$arr1 = ['yellow' => 1, 'red' => 5];
$arr2 = ['gray' => 3, 'black' => 8, 'yellow' => 3, 'orange' => 6];

$arrays = ['arr1','arr2'];
$merged = [];

foreach ($arrays as $array) {
  foreach (${$array} as $color => $value) {
    $merged[$color][] = [$value,$array];
  }
}

echo '<pre>';
print_r($merged);
echo '</pre>';

保持&#39;黄色&#39;的两个值。我介绍了一个额外的数组层。我不知道如何保留它们。

所以代码经过测试并返回:

Array
(
    [yellow] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => arr1
                )
            [1] => Array
                (
                    [0] => 3
                    [1] => arr2
                )
        )
    [red] => Array
        (
            [0] => Array
                (
                    [0] => 5
                    [1] => arr1
                )
        )
    [gray] => Array
        (
            [0] => Array
                (
                    [0] => 3
                    [1] => arr2
                )
        )
    [black] => Array
        (
            [0] => Array
                (
                    [0] => 8
                    [1] => arr2
                )
        )
    [orange] => Array
        (
            [0] => Array
                (
                    [0] => 6
                    [1] => arr2
                )
        )
)

答案 2 :(得分:0)

基于@arkascha回答处理重复的版本:

<?php
  $input = [
    'arr1'=> ['yellow' => 1,'red' => 5, 'black' => 4],
    'arr2'=> ['gray' => 3,'yellow' => 3, 'black' => 8,'orange' => 6],
    'arr3'=> ['orange' => 2]
  ];

  $output = [];
  array_walk($input, function ($entry, $source) use (&$output) {
    foreach ($entry as $key=>$val) {
      if(!array_key_exists($key, $output))
        $output[$key] = [[$val, $source]];
      else
        $output[$key][] = [$val, $source];
    }
  });

  echo print_r($output);

?>

PHP FIDDLE