合并特定的密钥的两个多维数组(Merging two multidimensional arrays

2019-07-04 09:04发布

比方说,我有以下阵列:

Array
    (
        [0] => Array
            (
                [id] => 5
                [name] => Education
            )

        [1] => Array
            (
                [id] => 4
                [name] => Computers
            )

        [3] => Array
            (
                [id] => 7
                [name] => Science

        [4] => Array
            (
                [id] => 1
                [name] => Sports
            )
    )

而第二个:

Array
    (
        [0] => Array
            (
                [id] => 1
                [title] => Sport
            )

        [1] => Array
            (
                [id] => 7
                [title] => Sci
            )

        [3] => Array
            (
                [id] => 4
                [title] => Comp

        [4] => Array
            (
                [id] => 5
                [title] => Edu
            )
    )

和期望的输出是:

Array
    (
        [0] => Array
            (
                [id] => 5
                [name] => Education
                [title] => Edu
            )

        [1] => Array
            (
                [id] => 4
                [name] => Computers
                [title] => Comp
            )

        [3] => Array
            (
                [id] => 7
                [name] => Science
                [title] => Sci

        [4] => Array
            (
                [id] => 1
                [name] => Sports
                [title] => Sport
            )
    )

我已成功地用简单的合并这些阵列:

foreach($first as $key => $value){
    $result[$key] = array_merge($first[$key], $second[$key]);
}

但输出不正确组合:

Array
    (
        [0] => Array
            (
                [id] => 5
                [name] => Education
                [title] => Sport
            )

        [1] => Array
            (
                [id] => 4
                [name] => Computers
                [title] => Sci
            )

        [3] => Array
            (
                [id] => 7
                [name] => Science
                [title] => Comp

        [4] => Array
            (
                [id] => 1
                [name] => Sports
                [title] => Edu
            )
    )

问题是我想在同一个合并这些阵列id 。 期望的输出排序应当与第一阵列英寸

我怎样才能做到这一点? 任何帮助深表感谢。

Answer 1:

你可以做一个嵌套循环,并检查id值匹配,然后添加title$first (或name$second

foreach($first as $key => $value){
    foreach($second as $value2){
        if($value['id'] === $value2['id']){
            $first[$key]['title'] = $value2['title'];
        }               
    }
}


Answer 2:

您的代码工作正常。 你的期望是根本不正确。 例如,在一个阵列第4单元id持有1,但在另一个数组,第4单元id为5,那么你的“合并在同一个ID这些阵列”是没有意义的,通过合并四元素融入其中,你也合并他们的孩子,因为id在两个阵列中使用,一旦值必须消失,因为不能在阵列中两个相等的键。

编辑

你必须手动合并的同时,你要根据内容合并基于 PHP函数合并:

$result = array();
foreach( $arrayA as $keyA => $valA ) {
  foreach( $arrayB as $keyB => $valB ) {
     if( $valA['id'] == $valB['id'] ) {
       $result[$keyA] = $valA + $valB;

       // or if you do not care output keys, just
       // $result[] = $valA + $valB;
     }
  }
}


Answer 3:

只要这两个数组总是有他们每一个ID,怎么样通过“ID”字段排序两个数组,然后让PHP执行合并?

function cmp($a, $b) {
  return ((int) $a['id'] < (int) $b['id']) ? -1 : 1;
}

usort($array1, 'cmp');
usort($array2, 'cmp');

$result = array_merge($array1, $array2);

没有测试的代码,但它表明了想法。



Answer 4:

不要在使用的foreach的foreach,这可能是当数组这么大太慢。

$idArray = array_column($secondArray,'title','id');
foreach($firstArray as $key => $val){
  $firstArray[$key]['title'] = (isset($idArray[$val['id']]) ? $idArray[$val['title'] : 'some title';
}


Answer 5:

为了提供可用的替代方法PHP 5.5+

由于两个阵列的顺序不相同,首先使用array_column索引阵列由id

这将允许您使用array_replace_recusrive的id索引的数组。

array_replace_recursive将在匹配两个阵列组的索引相关联地合并数组中的值。

任选使用array_values重新索引数组,取出id索引关联。

例如https://3v4l.org/ndv2j

$first = array_column($first, null, 'id');
$second = array_column($second, null, 'id');
$result = array_values(array_replace_recursive($first, $second));

结果

Array
(
    [0] => Array
        (
            [id] => 5
            [name] => Education
            [title] => Edu
        )

    [1] => Array
        (
            [id] => 4
            [name] => Computers
            [title] => Comp
        )

    [2] => Array
        (
            [id] => 7
            [name] => Science
            [title] => Sci
        )

    [3] => Array
        (
            [id] => 1
            [name] => Sports
            [title] => Sport
        )

)


Answer 6:

确保项目按相同的顺序,那么:

$items = array_map(function($itemFirstArray, $itemSecondArray) {
  return array_merge($itemFirstArray, $itemSecondArray);
}, $firstArray, $secondArray);


文章来源: Merging two multidimensional arrays on specific key