gpt4 book ai didi

php - 组合数组但保留键?

转载 作者:行者123 更新时间:2023-12-04 05:13:55 26 4
gpt4 key购买 nike

我正在尝试组合两个数组,但将它们的键保持在原位。

例如,我的 $artist_ids 数组如下:

Array
(
[1] => Array
(
[artist_id] => 12291
[artist_name] => Maroon 5
)
)

我有我的 $song_ids 数组:
Array
(
[0] => Array
(
[id] => 113064
[title] => Harder To Breathe
[artist_id] => 12291
[artist_name] => Maroon 5
)

[2] => Array
(
[id] => 113065
[title] => This Love
[artist_id] => 12291
[artist_name] => Maroon 5
)

[3] => Array
(
[id] => 113066
[title] => Shiver
[artist_id] => 12291
[artist_name] => Maroon 5
)
}

现在,如果我使用 array_merge($artist_ids, $song_ids),它看起来像是创建了一个新数组并将 $song_ids 附加到我的 $artist_ids 底部;完全无视键—— $artist_ids[1] 突然变成 [0] ,其余的跟在后面。

我可以用什么来固定 key ?我希望我的输出看起来像:
Array
(
[0] => Array
(
[id] => 113064
[title] => Harder To Breathe
[artist_id] => 12291
[artist_name] => Maroon 5
)
[1] => Array
(
[artist_id] => 12291
[artist_name] => Maroon 5
)

[2] => Array
(
[id] => 113065
[title] => This Love
[artist_id] => 12291
[artist_name] => Maroon 5
)

[3] => Array
(
[id] => 113066
[title] => Shiver
[artist_id] => 12291
[artist_name] => Maroon 5
)
}

最佳答案

只需使用重载 +合并数组:

$new_array = $artist_ids + $song_ids;

在此处查看操作: http://codepad.viper-7.com/1bJAfH

关于php - 组合数组但保留键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14554674/

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