2

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

例如,我的 $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] ,其余部分紧随其后。

我可以使用什么来固定钥匙?我希望我的输出看起来像:

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
    )
}
4

1 回答 1

3

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

$new_array = $artist_ids + $song_ids;

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

于 2013-01-28T02:38:18.950 回答