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

时间:2014-03-07 11:55:13

标签: php arrays sorting

我的第一个数组是

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 
    )

)

第二个数组是:

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 
    )

)

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

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 个答案:

答案 0 :(得分:1)

只需使用array_merge - http://bd1.php.net/array_merge

即可完成此操作
$res = array_merge($arr1, $arr2);

它会保留$arr1中重复的$arr2密钥。

答案 1 :(得分:0)

数组合并然后使用数组值

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