1

I'm using Symfony2 and Doctrine to get an array of values from the database. I want a list of values from one field of the database table.

I'm using the following code:

        return $this->createQueryBuilder('ad')
        ->select('ad.extId')
        ->where('ad.extId is not NULL')
        ->getQuery()
        ->getArrayResult();

It returns the following array:

Array
(
    [0] => Array
        (
            [extId] => 3038
        )
)

I would like to revieve the following:

Array
(
    [0] => 3038
)

Any one an idee to get this direct from Doctrine without an extra foreach loop?

1

1 Answer 1

1

Without custom hydrator, I don't think it is possible out of the box. However, you could do this (similar to foreach):

$arr = $this->createQueryBuilder('ad')
        ->select('ad.extId')
        ->where('ad.extId is not NULL')
        ->getQuery()
        ->getArrayResult();

return array_map(function($a){ return $a['extId']; }, $arr);
Sign up to request clarification or add additional context in comments.

3 Comments

For PHP5.5+ there's array_column() function which may be used instead of array_map here. array_map($arr, 'extId'); is simpler and actually has better performance.
Sweet :) And you meant array_column($arr, 'extId') ;)
Yeah, that's what I meant. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.