gol*_*ife 4 php arrays symfony doctrine-orm
使用存储库我得到了一个数组结果(每个数组都是一个实体对象),如下所示:
array(
0 => object of type entity,
1 => another object of type entity,
2 => another object of type entity,
)
Run Code Online (Sandbox Code Playgroud)
每个对象都有一些属性,如id和name等.但我想要的只是用每个对象的id展平整个数组.
我想要的是这个(仅使用ID's flatten the array):
Array
(
[0] => 1
[1] => 6
[2] => 23
)
Run Code Online (Sandbox Code Playgroud)
我的解决方案
$ids = array_map($transform = function($entity) {
if ($entity instanceof Entity) {
return $entity->getId();
}
}, $myGreatDbResult);
Run Code Online (Sandbox Code Playgroud)
我的解决方案正在运行,但是有更好的方法来获得这个结果吗?
yce*_*uto 11
一旦获得标识符数组,[0 => ['id' => 1], 1 => ['id' => 6], 2 => ['id' => 26] ...]就必须使用array_column函数从输入数组中的单个列获取值:
$ids = array_column($result, 'id');
Run Code Online (Sandbox Code Playgroud)
自PHP 5.5.0起
输出:
Array
(
[0] => 1
[1] => 6
[2] => 23
...
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1811 次 |
| 最近记录: |