有没有办法将两维数组转换为单维数组而不使用php中的foreach循环。
下面是实际的数组
Array
(
[0] => Array
(
[male] => male
[female] => female
)
[1] => Array
(
[male] => male1
[female] => female1
)
)
输出就像
Array
(
[0] = > male
[1] = > female
[2] = > male1
[3] = > female1
)
答案 0 :(得分:3)
您可以使用reduce
并使用array_merge
$array = array( ... ); //Your array here
$result = array_reduce($array, function($c,$v){
return array_merge(array_values($c),array_values($v));
}, array());
这将导致:
Array
(
[0] => male
[1] => female
[2] => male1
[3] => female1
)
答案 1 :(得分:0)
循环遍历多维数组并将结果存储在新数组变量$ newArray中。
$newArray = array();
foreach($multi as $array) {
foreach($array as $k=>$v) {
$newArray[$k] = $v;
}
}