doujiao7325 2013-09-03 11:33
浏览 115
已采纳

如何在现有的关联数组中插入新的键值对?

I'm having following associative array:

Array
(
    [0] => Array
        (
            [user_id] => 9def02e6337b888d6dbe5617a172c18d
            [user_first_name] => Ashutosh
            [user_last_name] => Modi
            [user_email] => ashutosh.modi@gmail.com
            [user_status] => enable
            [user_subscription] => lifetime
            [user_registered_type] => online
            [user_reg_date] => 1325581397
        )

    [1] => Array
        (
            [user_id] => a6d22e4cc3f65778a60b359842bcec82
            [user_first_name] => Dilip
            [user_last_name] => Modi
            [user_email] => dm.modi@gmail.com
            [user_status] => enable
            [user_subscription] => period
            [user_registered_type] => online
            [user_reg_date] => 1325152066
        )


)

It is having two keys. Now I'm having another array named $user_data which has the following output:

Array
(
    [user_state] => Rajasthan
    [user_city] => Jhunjhunu
)

I want to add this key value pair to the above array. Above array is just first iteration of for loop so the above values get inserted into first array element. the second array element will have somethong different values. I want the result in following manner. Can you help me in achieving this? Thanks in advance.

Array
    (
        [0] => Array
            (
                [user_id] => 9def02e6337b888d6dbe5617a172c18d
                [user_first_name] => Ashutosh
                [user_last_name] => Modi
                [user_email] => ashutosh.modi@gmail.com
                [user_status] => enable
                [user_subscription] => lifetime
                [user_registered_type] => online
                [user_reg_date] => 1325581397
                [user_state] => Rajasthan
                [user_city] => Jhunjhunu
            )

        [1] => Array
            (
                [user_id] => a6d22e4cc3f65778a60b359842bcec82
                [user_first_name] => Dilip
                [user_last_name] => Modi
                [user_email] => dm.modi@gmail.com
                [user_status] => enable
                [user_subscription] => period
                [user_registered_type] => online
                [user_reg_date] => 1325152066
                 [user_state] => Tamilnadu
                [user_city] => Chennai
            )


    )
  • 写回答

2条回答 默认 最新

  • douyan1970 2013-09-03 11:35
    关注
    <?php
        foreach($user_data as $key => $val){
            $original_array[$key]['user_state'] = $val['user_state'];
            $original_array[$key]['user_city'] = $val['user_city'];
        }
    

    assuming the 2 arrays have the same keys

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集