How to get a one-dimensional scalar array as a doctrine dql query result?(如何得到一维标量数组作为dql查询结果?)
问题描述
我想从 Auction 表的 id 列中获取一组值.如果这是一个原始 SQL,我会写:
从拍卖中选择id但是当我在 Doctrine 中这样做并执行时:
$em->createQuery("SELECT a.id FROM Auction a")->getScalarResult();我得到一个这样的数组:
数组(数组('id' => 1),数组('id' => 2),)相反,我想要一个这样的数组:
数组(1、2)我如何使用 Doctrine 做到这一点?
PHP 5.5
您可以使用 array_map,并且由于每个数组只有一个项目,因此您可以优雅地使用'current' 作为回调,而不是写一个 闭包.
$result = $em->createQuery("SELECT a.id FROM Auction a")->getScalarResult();$ids = array_map('current', $result);<块引用>
有关内存使用的其他信息,请参阅Petr Sobotka 在下面的回答.
PHP >= 5.5
作为 jcbwlkr 在下面的回答,推荐使用 array_column 的方式.
I want to get an array of values from the id column of the Auction table. If this was a raw SQL I would write:
SELECT id FROM auction
But when I do this in Doctrine and execute:
$em->createQuery("SELECT a.id FROM Auction a")->getScalarResult();
I get an array like this:
array(
array('id' => 1),
array('id' => 2),
)
Instead, i'd like to get an array like this:
array(
1,
2
)
How can I do that using Doctrine?
PHP < 5.5
You can use array_map, and since you only have on item per array, you can elegantly use
'current' as callback, instead of writing a closure.
$result = $em->createQuery("SELECT a.id FROM Auction a")->getScalarResult();
$ids = array_map('current', $result);
See Petr Sobotka's answer below for additional info regarding memory usage.
PHP >= 5.5
As jcbwlkr's answered below, the recommended way it to use array_column.
这篇关于如何得到一维标量数组作为dql查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何得到一维标量数组作为dql查询结果?
基础教程推荐
- Web 服务器如何处理请求? 2021-01-01
- php中的PDF导出 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- 将变量从树枝传递给 js 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
