php 将数组的php数组转换为单个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2289475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting php array of arrays into single array
提问by jhchen
I have an array whose values are all arrays of a specific format that looks like this:
我有一个数组,其值都是特定格式的数组,如下所示:
Array
(
[0] => Array
(
[username] => John
)
[1] => Array
(
[username] => Joe
)
[2] => Array
(
[username] => Jake
)
)
and I would like to have this:
我想要这个:
Array
(
[0] => John
[1] => Joe
[2] => Jake
)
I can do this manually with a loop but is there a better way? If not, is it possible to do this for an array of objects that have a common attribute?
我可以使用循环手动执行此操作,但有更好的方法吗?如果没有,是否可以对具有共同属性的对象数组执行此操作?
回答by Peter Taylor
Since PHP 5.5.0 there is a built-in function array_columnwhich does exactly this.
从 PHP 5.5.0 开始,有一个内置函数array_column可以做到这一点。
回答by SilentGhost
why complicate things?
为什么要把事情复杂化?
foreach($array as $k=>$v) {
$new[$k] = $v['username'];
}
回答by Manoj Thapliyal
$new_array = array_column($last_array, 'username');
回答by vfsoraki
For other readers, like myself, if the key usernameis different for each internal array, and you want to flatten it, use this:
对于像我这样的其他读者,如果username每个内部数组的键不同,并且您想将其展平,请使用以下命令:
call_user_func_array('array_merge', $the_array);
Note that the keys have to be different. Like this:
请注意,密钥必须不同。像这样:
[
0 => [
'a' => 1
],
1 => [
'b' => 2
]
]
After running the code, you have this:
运行代码后,你有这个:
[
'a' => 1,
'b' => 2,
]
回答by xpoback
Since PHP 5.6 you can use the splat operator. In case array keys are numeric, you could use this:
从 PHP 5.6 开始,您可以使用 splat 运算符。如果数组键是数字,你可以使用这个:
$newArray = array_merge(...$oldArray);
回答by Luiz Damim
If you're using PHP 5.3 you can make use of array_walk_recursiveand a closure (the closure can be replaced by a normal function if your PHP version < 5.3):
如果您使用的是 PHP 5.3,您可以使用array_walk_recursive和一个闭包(如果您的 PHP 版本 < 5.3,该闭包可以用普通函数替换):
function arrayFlatten(array $array) {
$flatten = array();
array_walk_recursive($array, function($value) use(&$flatten) {
$flatten[] = $value;
});
return $flatten;
}
$nested = array(
array('username' => 'John'),
array('username' => 'Joe'),
array('username' => 'Jake'),
);
$flattened = arrayFlatten($nested);
var_dump($flattened)
array
0 => string 'John' (length=4)
1 => string 'Joe' (length=3)
2 => string 'Jake' (length=4)
回答by thetaiko
$new = array_map(create_function('$auser', 'return $auser["username"];'), $array);
If using objects you could just change return $auser["username"];to return $auser->get_username();
如果使用对象,您可以更改return $auser["username"];为return $auser->get_username();
回答by Pavunkumar
Try this code:
试试这个代码:
foreach ($arr as $key => $val)
{
sort($val );
$new = array_merge($val, $new);
}
print_r ($new);
回答by smarber
For those who're using composer:
对于正在使用的人composer:
- if <=php5.5
array_columncan be used - if >php5.5 available polyfills such as https://github.com/symfony/polyfill/tree/master/src/Php55can be used. Once you
composer install symfony/polyfill-php55, then you can just callarray_columnas if your php5.4 or below had it.
- if <=php5.5
array_column可以使用 - 如果 > php5.5 可用的 polyfills 如https://github.com/symfony/polyfill/tree/master/src/Php55可以使用。一旦你
composer install symfony/polyfill-php55,那么你就可以array_column像你的 php5.4 或更低版本一样调用它。
回答by Nanhe Kumar
Try this:
尝试这个:
$list=array();
foreach($array as $v) {
array_push($list, $v['username']);
}

