合并来自不同键的共享值上的两个2d数组


Merge two 2d arrays on shared value from different keys

I有两个数组是这样设置的。

第一阵列

Array
(
    [0] => Array
        (
            [manufacture] => Moto
            [name] => ZSX 125
            [code] => ZS125-48A
            [cc] => 125
            [bike_type] => 3 Motorcycle
            [title] => Mto ZSX 125 
            [about] => The ZSX 
        )
    [1] => Array
        (
            [manufacture] => Moto
            [name] => LSM 125
            [code] => STR125YB
            [cc] => 125
            [bike_type] => 6 Endurancemotor
            [title] => Moto 
            [about] => Moto 
        )
)

第二阵列

Array
(
    [0] => Array
        (
            [id] => 183
            [model] => ZS125-48A
            [engine_brand] => 158FMI-B
            [engine_type] => Single Cylinder
        )
    [1] => Array
        (
            [id] => 172
            [model] => STR125YB
            [engine_brand] => K154FMI
            [engine_type] => Single Cylinder
        )
)

正如您所看到的,第一个数组中的"code"与第二个数组中"model"相同,并且始终存在匹配。

这是我想要的数组。

Array
(
    [0] => Array
        (
            [id] => 183
            [model] => ZS125-48A
            [engine_brand] => 158FMI-B
            [engine_type] => Single Cylinder
            [manufacture] => Moto
            [name] => ZSX 125
            [code] => ZS125-48A
            [cc] => 125
            [bike_type] => 3 Motorcycle
            [title] => Moto ZSX 125 
            [about] => The ZSX 
        )
    [1] => Array
        (
            [id] => 172
            [model] => STR125YB
            [engine_brand] => K154FMI
            [engine_type] => Single Cylinder
            [manufacture] => Moto
            [name] => LSM 125
            [code] => STR125YB
            [cc] => 125
            [bike_type] => 6 Endurancemotor
            [title] => Moto 
            [about] => Moto 
        )
)

这可能吗?我尝试过arraymerge和arraymerge_recurive,但它只将第二个数组附加到第一个数组的末尾,而没有在键上合并。

Decze的解决方案是完美的,如果两个数组是同步的(即具有匹配codemodel的项在两个数组中处于同一索引)。如果不是这样,您将需要不止一行代码。

数组需要具有字符串键才能与array_merge合并。在这种情况下,您希望基于code重新密钥第一个,基于model重新密钥第二个。重新密钥后,您可以将它们与array_merge_recursive合并。

重新键入在PHP 5.5+中很容易:

// array_column will conveniently rekey when the second argument is null
$array1 = array_column($array1, null, 'code');
$array2 = array_column($array2, null, 'model');

在早期版本中有点复杂:

// array_map is a more complicated way to extract a column from an array
// and array_combine will do the rekeying
$array1 = array_combine(
              array_map(function($i) { return $i['code']; }, $array1),
              $array1);
$array2 = array_combine(
              array_map(function($i) { return $i['model']; }, $array2),
              $array2);

在这两种情况下,最后一步都是一样的:

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

在我看来应该这样做:

$array3 = array_map('array_merge', $array1, $array2);

这将$array1[0]$array2[0]馈送到array_merge,然后是$array1[1]$array2[1]等。

如果您喜欢"更简单"的代码…:-)

$result = array();
foreach ($one as $a) {
    foreach ($two as $b) {
        if ($a->code == $b->model) {
           array_push($result, array_merge($a, $b));
           break;
        }
    }
}
print_r($result);