在 PHP 中连接多个数组元素
我有一个数组“$abc”,它有 9 个元素,如下所示: -
Array
(
[a] => Jack
[b] => went
[c] => up
[d] => the
[e] => hill
[f] => but
[g] => never
[h] => came
[i] => back
)
现在我只需要连接从“b”索引到“e”索引的 4 个元素。但我不知道该怎么办。在连接所有数组元素的情况下,我使用了 PHP 的“implode()”函数。
非常感谢任何帮助。
I have an array "$abc" which has 9 elements, as:-
Array
(
[a] => Jack
[b] => went
[c] => up
[d] => the
[e] => hill
[f] => but
[g] => never
[h] => came
[i] => back
)
Now I need to concat only the 4 elements starting from the "b" index to the "e" index only. But I don't know what to do. I used the "implode()" function of PHP in cases where all the array elements are concatenated.
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要先提取所需的值,然后使用
implode
。您可以使用array_slice
:这会产生
上山
。如果您需要使用文字键,则需要更有创意。在你的情况下,最好只是循环遍历数组并进行比较,但你也可以做一些奇特的事情:
You need to extract the desired values first and then use
implode
. You could usearray_slice
:That would produce
went up the hill
.If you need to work with the literal keys, you need to be a bit more creative. In your case it would probably best just to loop through the array and compare, but you can do something exotic also: