How to get a one-dimensional scalar array as a doctrine dql query result?

后端 未结 5 1533
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 06:20

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 w

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 06:54

    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.

提交回复
热议问题