2

我有以下关联数组:

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
        )


)

它有两个键。现在我有另一个名为的数组$user_data,它具有以下输出:

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

我想将此键值对添加到上面的数组中。上面的数组只是 for 循环的第一次迭代,所以上面的值被插入到第一个数组元素中。第二个数组元素将有一些不同的值。我想要以下方式的结果。你能帮我实现这个目标吗?提前致谢。

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
            )


    )
4

2 回答 2

3

尝试array_merge

foreach ($users as &$user)
{
    $user = array_merge($user, $user_data);
}

var_dump($users);
于 2013-09-03T11:38:28.817 回答
2
<?php
    foreach($user_data as $key => $val){
        $original_array[$key]['user_state'] = $val['user_state'];
        $original_array[$key]['user_city'] = $val['user_city'];
    }

假设 2 个数组具有相同的键

于 2013-09-03T11:35:57.777 回答