将多维数组转为一维数组
我已经在这个问题上摸索了一段时间了。
我有这个多维数组:
Array
(
[0] => Array
(
[0] => foo
[1] => bar
[2] => hello
)
[1] => Array
(
[0] => world
[1] => love
)
[2] => Array
(
[0] => stack
[1] => overflow
[2] => yep
[3] => man
)
我需要得到这个:
Array
(
[0] => foo
[1] => bar
[2] => hello
[3] => world
[4] => love
[5] => stack
[6] => overflow
[7] => yep
[8] => man
)
有什么想法吗?
我发现的所有其他解决方案都解决了具有不同键的多维数组。我的数组仅使用简单的数字键。
I've been banging my head on this one for a while now.
I have this multidimensional array:
Array
(
[0] => Array
(
[0] => foo
[1] => bar
[2] => hello
)
[1] => Array
(
[0] => world
[1] => love
)
[2] => Array
(
[0] => stack
[1] => overflow
[2] => yep
[3] => man
)
And I need to get this:
Array
(
[0] => foo
[1] => bar
[2] => hello
[3] => world
[4] => love
[5] => stack
[6] => overflow
[7] => yep
[8] => man
)
Any ideas?
All other solutions I found solve multidimensional arrays with different keys. My arrays use simple numeric keys only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
示例:
结果:
Example:
Result:
PHP
array_merge
文档函数可以展平你的数组:如果原始数组的深度高于 2 层,PHP 中的 SPL 有一个
RecursiveArrayIterator
您可以使用它来展平它:另请参阅:如何展平多维数组?。
The PHP
array_merge
Docs function can flatten your array:In case the original array has a higher depth than 2 levels, the SPL in PHP has a
RecursiveArrayIterator
you can use to flatten it:See as well: How to Flatten a Multidimensional Array?.
从 PHP 5.6 开始这可以是使用 参数更简单地完成拆包。
As of PHP 5.6 this can be done more simply with argument unpacking.
这就是它的全部内容:
This is really all there is to it:
其中 $a 是您的数组名称。
了解详情
where $a is your array name.
for details
从 PHP 5.3 开始,最短的解决方案似乎是带有新闭包语法的 array_walk_recursive() :
As of PHP 5.3 the shortest solution seems to be array_walk_recursive() with the new closures syntax:
在 PHP5.6 中,还有其他方法来解决这个问题,结合函数,
array_shift()
删除原始数组的第一个元素,array_pus()
添加项目新数组,重要的是 of...
slapt/elipse 运算符,它将像参数一样解压array_shitf()
的返回值。输出:
示例 - ideone
In PHP5.6 there other way to solve this problem, combining the functions,
array_shift()
to remove the first elemente of the original array,array_pus()
to add items an new array, the important thing is the of...
splapt/elipse operator it will unpack the return ofarray_shitf()
like an argument.Output:
Example - ideone
我曾使用此代码来解决相同类型的问题。所以你也可以尝试这个。
I had used this code to resolve same type of problem. so you can also try this.
这将使
This will make
结果(一维数组):
RESUTL(ONE DIMENTIONAL ARRAY) :
最快的解决方案是使用这个 数组库:
它将生成您想要的数组
The quickest solution would be to use this array library:
which will produce exactly the array you want