如何展平多维数组? [英] How to Flatten a Multidimensional Array?

查看:40
本文介绍了如何展平多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHP 中,是否可以在不使用递归或引用的情况下展平(双/多)维数组?

Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references?

我只对值感兴趣,所以可以忽略键,我在考虑 array_map()array_values() 的行.

I'm only interested in the values so the keys can be ignored, I'm thinking in the lines of array_map() and array_values().

推荐答案

您可以使用 标准 PHP 库 (SPL)隐藏"递归.

You can use the Standard PHP Library (SPL) to "hide" the recursion.

$a = array(1,2,array(3,4, array(5,6,7), 8), 9);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
foreach($it as $v) {
  echo $v, " ";
}

印刷品

1 2 3 4 5 6 7 8 9 

这篇关于如何展平多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆