结合阵列但保留钥匙? [英] combining arrays but keep keys?
问题描述
我想两个阵列组合,但保留其键到位。
比如说,我有我的下面$ artist_ids数组:
阵列
(
[1] =>排列
(
[artist_id] => 12291
[ARTIST_NAME] => Maroon 5的
)
)
和我有我的$ song_ids数组:
阵列
(
[0] =>排列
(
[ID] => 113064
[标题] =>难以呼吸
[artist_id] => 12291
[ARTIST_NAME] => Maroon 5的
)[2] =>排列
(
[ID] => 113065
[标题] =>这份爱
[artist_id] => 12291
[ARTIST_NAME] => Maroon 5的
)[3] =>排列
(
[ID] => 113066
[标题] =>发抖
[artist_id] => 12291
[ARTIST_NAME] => Maroon 5的
)
}
现在,如果我用array_merge($ artist_ids,$ song_ids),它看起来像它创建$ song_ids我的$ artist_ids的底部有一个新的阵列和图钉;全然不顾键 - $ artist_ids [1]是突然[0],其余的拖影
。我可以使用保持键到位?想我我的输出看起来像:
阵列
(
[0] =>排列
(
[ID] => 113064
[标题] =>难以呼吸
[artist_id] => 12291
[ARTIST_NAME] => Maroon 5的
)
[1] =>排列
(
[artist_id] => 12291
[ARTIST_NAME] => Maroon 5的
)[2] =>排列
(
[ID] => 113065
[标题] =>这份爱
[artist_id] => 12291
[ARTIST_NAME] => Maroon 5的
)[3] =>排列
(
[ID] => 113066
[标题] =>发抖
[artist_id] => 12291
[ARTIST_NAME] => Maroon 5的
)
}
只需使用重载的 +
合并数组:
$ new_array = $ artist_ids + $ song_ids;
在这里看到它在行动: HTTP://$c$cpad.viper-7。 COM / 1bJAfH
I'm trying to combine two arrays but keep their keys in place.
For instance, I have my $artist_ids array below:
Array
(
[1] => Array
(
[artist_id] => 12291
[artist_name] => Maroon 5
)
)
And I have my $song_ids array:
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
)
}
Now, if I use array_merge($artist_ids, $song_ids), it looks like it creates a new array and tacks on $song_ids to the bottom of my $artist_ids; totally disregarding the keys -- $artist_ids[1] is suddenly [0] with the rest trailing after.
What can I use that keeps the keys in place? I'd like my output to look like:
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
)
}
Just use the overloaded +
to merge the arrays:
$new_array = $artist_ids + $song_ids;
See it here in action: http://codepad.viper-7.com/1bJAfH
这篇关于结合阵列但保留钥匙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!