将数组的php数组转换为单个数组
问题描述:
我有一个数组,其值都是特定格式的数组,如下所示:
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
)
)
我想要这个:
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?
答
从 PHP 5.5.0 开始就有一个内置函数 array_column
就是这样做的.
Since PHP 5.5.0 there is a built-in function array_column
which does exactly this.