最新消息:本站所有跳转向bbs.ykit.cn的附件将全面停止,附件已转移到https://www.qingsj.com

php快速高效的把多维数组变为一维数组

Mysql 有客 427浏览 0评论
直接上函数:

function reduceArray($array) {
$return = [];
array_walk_recursive($array, function ($x) use (&$return) {
$return[] = $x;
});
return $return;
}
*******************
$a = [
[[1, 2, 3], [2, 3, 4]],
[[1, 2, 3], [2, 3, [2, 3, [22, 333, 444], 4, 88, 99], 4]],
[[1, 2, 3], [2, 3, 4]],
];
echo "<pre>";
print_r(reduceArray($a));
echo "</pre>";
exit;

*****************************
结果如下:

<pre>Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 2
[4] => 3
[5] => 4
[6] => 1
[7] => 2
[8] => 3
[9] => 2
[10] => 3
[11] => 2
[12] => 3
[13] => 22
[14] => 333
[15] => 444
[16] => 4
[17] => 88
[18] => 99
[19] => 4
[20] => 1
[21] => 2
[22] => 3
[23] => 2
[24] => 3
[25] => 4
)
</pre>[Finished in 0.1s]

转载请注明:有客帮 » php快速高效的把多维数组变为一维数组

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址