根据共享列值合并两个2d数组,并在附加元素时修改键



我有以下两个对象数组:
First Array: $array1

        Array
(
    [0] => Array
        (
            [match] => 1
            [when] => 2013-10-13 15:00:00
            [a] => AD
            [b] => NiP
            [winner] => c
            [closed] => 1
            [event] => Fragbite Masters
            [format] => 3
        )
    [1] => Array
        (
            [match] => 2
            [when] => 2013-10-13 15:00:00
            [a] => VG
            [b] => AD
            [winner] => a
            [closed] => 1
            [event] => Starseries
            [format] => 5
        )
    [2] => Array
        (
            [match] => 3
            [when] => 2013-10-13 21:15:00
            [a] => Serbia
            [b] => Portugal
            [winner] => a
            [closed] => 1
            [event] => ESEC
            [format] => 1
        )
)

Second Array: $array2

    Array
(
    [0] => Array
        (
            [match] => 1
            [a] => 58
            [b] => 107
        )
    [1] => Array
        (
            [match] => 2
            [a] => 174
            [b] => 162
        )
    [2] => Array
        (
            [match] => 3
            [a] => 64
            [b] => 59
        )
)

我想得到这样的东西:

    Array
(
    [0] => Array
        (
            [match] => 1
            [when] => 2013-10-13 15:00:00
            [a] => AD
            [b] => NiP
            [winner] => c
            [closed] => 1
            [event] => Fragbite Masters
            [format] => 3
            [per_a] => 58
            [per_b] => 107
        )
    [1] => Array
        (
            [match] => 2
            [when] => 2013-10-13 15:00:00
            [a] => VG
            [b] => AD
            [winner] => a
            [closed] => 1
            [event] => Starseries
            [format] => 5
            [per_a] => 174
            [per_b] => 162
        )
    [2] => Array
        (
            [match] => 3
            [when] => 2013-10-13 21:15:00
            [a] => Serbia
            [b] => Portugal
            [winner] => a
            [closed] => 1
            [event] => ESEC
            [format] => 1
            [per_a] => 64
            [per_b] => 59
        )
)

其中第二个数组中的键名[a][b]已被修改为[per_a][per_b]

事情我试图合并两个文件:
array_merge,array_merge_recursive:两者都得到一个结果,其中$array2的合并值被附加到$array1的末尾。
array_combine:不能工作,因为$array1$array2没有相同数量的元素。

这只是每个文件的一部分,两者在元素方面不相等。如果你有一个答案,我将不胜感激!

试试这个,您需要在函数中修改您的键。演示看到

$match:将检查key在两个数组中是否存在。$whereKey1
$whereKey2:从第二个数组中取出值并将其放入新建$newkey1$newkey2的key

function key_compare_func($arr1, $arr2)
{
    $newarray = array();
    $match = "match";
    $whereKey1 = "a";
    $whereKey2 = "b";
    $newKey1 = "per_a";
    $newKey2 = "per_b";
    if(is_array($arr1) && is_array($arr2)){
        if($arr1[$match] == $arr2[$match] ){
            $newarray = array_merge($arr1, array($newKey1 => $arr2[$whereKey1], $newKey2 => $arr2[$whereKey2]));
        }
    }
  return $newarray;
}
$modifiedArray = array_map("key_compare_func",$A1, $A2);
echo '<pre>';print_r($modifiedArray);echo '</pre>';
输出:

[0] => Array
        (
            [match] => 1
            [when] => 2013-10-13 15:00:00
            [a] => AD
            [b] => NiP
            [winner] => c
            [closed] => 1
            [event] => Fragbite Masters
            [format] => 3
            [per_a] => 58
            [per_b] => 107
        )
    [1] => Array
        (
            [match] => 2
            [when] => 2013-10-13 15:00:00
            [a] => VG
            [b] => AD
            [winner] => a
            [closed] => 1
            [event] => Starseries
            [format] => 5
            [per_a] => 174
            [per_b] => 162
        )

假设您的第二个数组只保存唯一的match id,那么您可以在将match值应用为第一级键后将其用作查找数组。array_column()使这个任务非常简单。

然后简单地遍历第一个数组中的行(通过引用修改,以便对行的更改应用于原始数组)。

使用这种技术,如果两个数组之间对应的行具有不同的原始第一级键,则无关紧要(@Noman的答案不能这样说,它要求第一个数组中的每一行在第二个数组中具有完全相同位置的对应行)。

代码(演示):

$lookup = array_column($A2, null, 'match');
foreach ($A1 as &$row) {
    if (isset($lookup[$row['match']])) {
        $row['per_a'] = $lookup[$row['match']]['a'];
        $row['per_b'] = $lookup[$row['match']]['b'];
    }
}
var_export($A1);

相关内容

  • 没有找到相关文章

最新更新