PHP - 合并值匹配的两个关联数组

时间:2018-05-17 06:25:52

标签: php arrays

我有两个需要合并的数组。我可以遍历每个数组并手动合并。但是,是否有内置函数来执行此操作?

数组1:

Array
(
[0] => Array
    (
        [detail_image_id] => 6389
        [product_name] => Starter broadband Package
    )

[1] => Array
    (
        [detail_image_id] => 6358
        [product_name] => Starter broadband Package
    )
)

数组2:

Array
(
[0] => Array
    (
        [detail_image_id] => 6358
        [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg
    )

[1] => Array
    (
        [detail_image_id] => 6389
        [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg
    )
)

预期输出数组是:

Array
(
[0] => Array
    (
        [detail_image_id] => 6389
        [product_name] => Starter broadband Package
        [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg
    )

[1] => Array
    (
        [detail_image_id] => 6358
        [product_name] => Starter broadband Package
        [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg
    )
)

我只添加了数组中的一个示例。阵列很大。是否有任何PHP函数,如array_merge()array_map(),我们可以使用而不是手动循环和迭代器?

3 个答案:

答案 0 :(得分:3)

您可以使用array_column()array_combine()的组合来创建使用detail_image_id作为关键字的参考数组。然后,使用foreach()循环,您可以使用+ operator合并数组:

$array1 = [
    ['detail_image_id' => '6389', 'product_name' => 'Starter broadband Package'],
    ['detail_image_id' => '6358', 'product_name' => 'Starter broadband Package']
];
$array2 = [
    ['detail_image_id' => '6358', 'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg'],
    ['detail_image_id' => '6389', 'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg']
];

// create associative array with detail_image_id as key,
$out = array_combine(array_column($array1, 'detail_image_id'), $array1);
foreach ($array2 as $item) {
    $key = $item['detail_image_id']; // shortcut for the key,
    $out[$key] = $out[$key] + $item; // merge arrays
}
print_r(array_values($out)); // reset to indexed keys (0,1,2...) 

输出:

Array
(
    [0] => Array
        (
            [detail_image_id] => 6389
            [product_name] => Starter broadband Package
            [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg
        )

    [1] => Array
        (
            [detail_image_id] => 6358
            [product_name] => Starter broadband Package
            [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg
        )

)

答案 1 :(得分:2)

您可以执行以下操作:

$arr1 = array(); //Your array 1
$arr2 = array(); //Your array 2

//Make a temp array to that use detail_image_id as the key
$arr1Temp = array_combine( array_column($arr1,'detail_image_id'), $arr1 );
$arr2Temp = array_combine( array_column($arr2,'detail_image_id'), $arr2 );

//Get All unique detail_image_id from 2 arrays. 
//This is to make sure that all detail_image_id's will be included. 
//detail_image_id on 2nd array might not be present on the 1st one
$imgIds = array_unique(array_merge( array_keys($arr1Temp), array_keys($arr2Temp) ));

//Do a simple foreach loop
$result = array();
foreach( $imgIds as $val ) {
    $result[] = array_merge( $arr1Temp[$val], $arr2Temp[$val] );
}

print_r( $result );

这将导致:

Array
(
    [0] => Array
        (
            [detail_image_id] => 6389
            [product_name] => Starter broadband Package
            [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg
        )

    [1] => Array
        (
            [detail_image_id] => 6358
            [product_name] => Starter broadband Package
            [guid] => http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg
        )

)

答案 2 :(得分:2)

array_merge_recursive()不太适合,因为遇到重复的detail_image_id值时会生成子数组。

要使用非常方便的array_replace_recursive()函数合并两个数组,必须先准备数组。

array_..._recursive()函数不能与索引数组很好地配合使用。因此,您需要使用detail_image_id分配临时密钥。但是等等,即使你将这些值转换为字符串,php也会将它们视为非关联,因为值是数字的。解决方法是添加一个可以脱离php"有用的字符#34;假设

合并数组后,使用array_values()重新索引第一级键。

代码:(Demo

$array1 = [
    ['detail_image_id' => '6389', 'product_name' => 'Starter broadband Package'],
    ['detail_image_id' => '6358', 'product_name' => 'Starter broadband Package']
];
$array2 = [
    ['detail_image_id' => '6358', 'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg'],
    ['detail_image_id' => '6389', 'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg']
];

foreach ($array1 as $row) {
    $keyed1["a{$row['detail_image_id']}"] = $row;
}
foreach ($array2 as $row) {
    $keyed2["a{$row['detail_image_id']}"] = $row;
}
var_export(array_values(array_replace_recursive($keyed1, $keyed2)));

输出:

array (
  0 => 
  array (
    'detail_image_id' => '6389',
    'product_name' => 'Starter broadband Package',
    'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg',
  ),
  1 => 
  array (
    'detail_image_id' => '6358',
    'product_name' => 'Starter broadband Package',
    'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg',
  ),
)

P.S。为了未来研究人员的利益,如果您已经将非数字值用作临时密钥,那么array_column()是最干净的。 (没理由拨打array_combine()。)

代码:(Demo

$array1 = [
    ['detail_image_id' => 'A', 'product_name' => 'Starter broadband Package'],
    ['detail_image_id' => 'B', 'product_name' => 'Starter broadband Package']
];
$array2 = [
    ['detail_image_id' => 'B', 'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-wingle.jpg'],
    ['detail_image_id' => 'A', 'guid' => 'http://xxx/wp-content/uploads/2018/04/broadband-4mbs-charji.jpg']
];

$keyed1 = array_column($array1, null, 'detail_image_id');
$keyed2 = array_column($array2, null, 'detail_image_id');

var_export(array_values(array_replace_recursive($keyed1, $keyed2)));