问题
I often have a 2-dimensional array:
array(
array('key' => 'value1'),
array('key' => 'value2'),
...
);
And need to form a 1-dimensional array:
array('value1', 'value2')
This can easily be done with foreach, but I wonder if there's some php 5.3 way to do it in one line.
回答1:
$new_array = array_map(function($el) { return $el['key']; }, $array);
回答2:
<?php
$arr = array(array(141,151,161,140),2,3,array(101,202,array(303,404),407));
function array_oned($arrays){
static $temp_array = array();
foreach($arrays as $key){
if(is_array($key)){
array_oned($key);
}else {
$temp_array [] = $key;
}
}
return $temp_array;
}
echo print_r(array_oned($arr));
?>
Did you mean something like this ?
回答3:
array_reduce($array,function($arr,$new){
$arr[]=$new['key'];
},array())
回答4:
In case, when you have only one value in inner arrays:
$values = array_map('array_pop', $yourArray);
Callback could be function name, so why reimplement something that already exists as a core function? :)
来源:https://stackoverflow.com/questions/6940217/multidimensional-array-to-array