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

后端 未结 5 1532
佛祖请我去吃肉
佛祖请我去吃肉 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 07:09

    Ascarius' answer is elegant, but beware of memory usage! array_map() creates a copy of passed array and effectively doubles memory usage. If you work with hundreds of thousands of array items this can become an issue. Since PHP 5.4 call-time pass by reference has been removed so you cannot do

    // note the ampersand
    $ids = array_map('current', &$result);
    

    In that case you can go with obvious

    $ids = array();
    foreach($result as $item) {
      $ids[] = $item['id'];
    }
    

提交回复
热议问题