如何在特定键上合并两个多维数组(唯一键)

时间:2022-01-30 18:17:10

My First array is

我的第一个阵列是

Array
(
[19] => Array
    (
        [entity_id] => 19
        [entity_type_id] => 4
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => Escape
        [has_options] => 1
        [required_options] => 0
        [created_at] => 2014-02-27 07:33:53
        [updated_at] => 2014-03-06 07:29:49
        [category_id] => 181
        [pgender] => male
        [name] => Escape 
    )

[32] => Array
    (
        [entity_id] => 32
        [entity_type_id] => 4
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => Laundry Service
        [has_options] => 1
        [required_options] => 0
        [created_at] => 2014-02-27 19:41:29
        [updated_at] => 2014-03-06 11:02:35
        [category_id] => 181
        [pgender] => both
        [name] => Laundry Service 
    )

)

And Second array is:

第二个数组是:

Array
(
[20] => Array
    (
        [entity_id] => 20
        [entity_type_id] => 4
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => Escape
        [has_options] => 1
        [required_options] => 0
        [created_at] => 2014-02-27 07:33:53
        [updated_at] => 2014-03-06 07:29:49
        [category_id] => 181
        [pgender] => male
        [name] => Escape name
    )

[32] => Array
    (
        [entity_id] => 32
        [entity_type_id] => 4
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => Laundry Service
        [has_options] => 1
        [required_options] => 0
        [created_at] => 2014-02-27 19:41:29
        [updated_at] => 2014-03-06 11:02:35
        [category_id] => 181
        [pgender] => both
        [name] => Laundry Service 
    )

)

I want to merge array by unique key:

我想通过唯一键合并数组:

Array
(
[1] => Array
    (
        [entity_id] => 19
        [entity_type_id] => 4
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => Escape
        [has_options] => 1
        [required_options] => 0
        [created_at] => 2014-02-27 07:33:53
        [updated_at] => 2014-03-06 07:29:49
        [category_id] => 181
        [pgender] => male
        [name] => Escape 
    )

[2] => Array
    (
        [entity_id] => 32
        [entity_type_id] => 4
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => Laundry Service
        [has_options] => 1
        [required_options] => 0
        [created_at] => 2014-02-27 19:41:29
        [updated_at] => 2014-03-06 11:02:35
        [category_id] => 181
        [pgender] => both
        [name] => Laundry Service 
    )
[3] => Array
    (
        [entity_id] => 20
        [entity_type_id] => 4
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => Escape
        [has_options] => 1
        [required_options] => 0
        [created_at] => 2014-02-27 07:33:53
        [updated_at] => 2014-03-06 07:29:49
        [category_id] => 181
        [pgender] => male
        [name] => Escape name
    )
)

2 个解决方案

#1


1  

This can be done simply with array_merge - http://bd1.php.net/array_merge

这可以使用array_merge - http://bd1.php.net/array_merge完成

$res = array_merge($arr1, $arr2);

It will preserve the keys from $arr1 that are duplicated in $arr2.

它将保留$ arr1中与$ arr2重复的键。

#2


0  

array merge and then use array values as below

数组合并然后使用如下的数组值

array_values(array_merge($array1 , $array1));

#1


1  

This can be done simply with array_merge - http://bd1.php.net/array_merge

这可以使用array_merge - http://bd1.php.net/array_merge完成

$res = array_merge($arr1, $arr2);

It will preserve the keys from $arr1 that are duplicated in $arr2.

它将保留$ arr1中与$ arr2重复的键。

#2


0  

array merge and then use array values as below

数组合并然后使用如下的数组值

array_values(array_merge($array1 , $array1));