douchi5822 2014-03-07 11:55
浏览 35

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

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条回答 默认 最新

  • dongshenghe1833 2014-03-07 12:06
    关注

    This can be done simply with 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上