gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:52:28 24 4
gpt4 key购买 nike

我有以下关联数组:

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
)


)

最佳答案

尝试使用 array_merge

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

var_dump($users);

关于php - 如何在现有的关联数组中插入一个新的键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18591227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com