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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?