转换多维数组到一个单一维的 [英] Converting a multidimensional array into a single dimensional one
问题描述
如果从一个函数,我现在回到这样一个多维数组。
阵列(0 =>阵列('A'=大于5),1 =>阵列('A'=→8))
不过,我真的很需要的关键内容A什么是我要转换的最佳方式。
目前我一直在做这样的事情。
$ new_array =阵列();的foreach($ multi_array的AS $行){
$ new_array [] = $行['一']
}
如果这是你所有的要求是,我认为这是最好的办法。还有什么都会有相同的处理。甚至通过阵列功能照看,我会说,这将是最好的方式。你可以,但是,使它成为一个功能,使多一点多才多艺的:
$阵列=阵列(0 =>阵列('A'=> 1),1 =>阵列('A'=> 8));$ new_array =弄平($阵列,'一个');功能扁平化($数组$指数='A'){
$收益率=阵列(); 如果(is_array($阵列)){
的foreach($数组作为$行){
$返回[] = $行[$指数]
}
} 返回$返回;
}
不过啊,我说你有什么是做的最有效的方式。
If from a function I am returned a multidimensional array like this..
array(0 => array('a' => 5), 1 => array('a' => 8))
But I just really need the contents of the key 'a' what is the best way for me to convert.
Current I have been doing something like..
$new_array = array();
foreach ($multi_array AS $row) {
$new_array[] = $row['a']
}
If that is all your requirements are, I think that is the best way. Anything else will have the same processing. Even after looking through the Array functions, I would say that this would be the best way. You can, however, make it a function to make it a bit more versatile:
$array = array(0 => array('a' => 1), 1 => array('a' => 8));
$new_array = flatten($array, 'a');
function flatten($array, $index='a') {
$return = array();
if (is_array($array)) {
foreach ($array as $row) {
$return[] = $row[$index];
}
}
return $return;
}
But yea, I would say what you have would be the most efficient way of doing it.
这篇关于转换多维数组到一个单一维的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!