PHP 递归N级数组,转换为一维数组
测试代码:
$data = array(
'1',
['张三', '账务'],
'name' => [
'test' => ['t1', 't2', 'list'=>['t4', 't11']]
]
);
var_dump(arrays_convert_one($data));
function arrays_convert_one($data) {
$result = array();
foreach ($data as $key => $val) {
if( is_array($val) ) {
$result = array_merge($result, arrays_convert_one($val));
} else {
$result[] = $val;
}
}
return $result;
}
运行结果:
array(7) {
[0]=>
string(1) “1”
[1]=>
string(6) “张三”
[2]=>
string(6) “账务”
[3]=>
string(2) “t1”
[4]=>
string(2) “t2”
[5]=>
string(2) “t4”
[6]=>
string(3) “t11”
}