10

With a repository I got an array result (each array is an entity object) like this:

array(
  0 => object of type entity,
  1 => another object of type entity,
  2 => another object of type entity,
)

each object has some properties like id and name, etc. But I what I want is flatten the whole array only with the id of each object.

What I want is this (flatten the array only with ID's):

Array
(
    [0] => 1
    [1] => 6
    [2] => 23
)

My solution:

$ids = array_map($transform = function($entity) {
    if ($entity instanceof Entity) {
       return $entity->getId();
    }
 }, $myGreatDbResult);

My solution is working but is there a better way to get this result?

4 Answers 4

22

Once you get the array of identifiers [0 => ['id' => 1], 1 => ['id' => 6], 2 => ['id' => 26] ...] just you have to use array_column function to get the values from a single column in the input array:

$ids = array_column($result, 'id');

Since PHP 5.5.0

Output:

Array
(
    [0] => 1
    [1] => 6
    [2] => 23
    ...
)
Sign up to request clarification or add additional context in comments.

1 Comment

it is now possible to use: $query->getSingleColumnResult() or $query->getResult(AbstractQuery::HYDRATE_SCALAR_COLUMN)
1

The result you are getting:

array(
  0 => object of type entity,
  1 => another object of type entity,
  2 => another object of type entity,
)

Is probably a result of findBy() or getResult() methods. To achieve what you want you will need to create your own query and do something like this:

$result = $entityManager->createQueryBuilder()
    ->from('AcmeDemoBundle:Entity', 'e') //your entity
    ->select('e.id') //your id field
    ->getQuery()
    ->getScalarResult();

This will give you array you're expecting

6 Comments

It will give an array [0 => ['id' => 1], 1 => ['id' => 6], 2 => ['id' => 26] ...] so array_map still needed.
did you try getScalarResult instead of getArrayResult ? I edited an answer
Yes, getScalarResult gives the same array as getArrayResult
Still, this approach is better as it avoids unnecessarily hydrating the objects.
the scalarResult will give me an associative array. But not the expected or wished result. When I combine your solution with the answer of yonel I got my wished result.
|
1

To do what you want you can now simply use

$qb->getSingleColumnResult()

Which returns a numeric array with each column value (omitting the column name etc)

As per comment from user KicksheepSon

Comments

-1

The best practices are to use the symfony native method, so as Tomasz Madeyski says, Use

...->getQuery()->getScalarResult()

You can also get it that way by doing

->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY)

A good practice also is to create the method INTO the repository class and make it hybrid by giving the choice of the two types, for exemple :.

public function findAll($hydratedArray = false)
{
    $query = $this  ->createQueryBuilder('p')
                    ->leftJoin('p.company', 'c')
                    ->select('p')
                    ->orderBy('c.name', 'ASC');

    $result = $hydratedArray    ? $query
                                    ->leftJoin('p.series', 's')
                                    ->leftJoin('s.variety', 'v')
                                    ->addSelect('c', 's', 'v')
                                    ->getQuery()
                                    ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY) // or getScalarResult()
                              : $query->getQuery()->getResult();

    return $result;
}

1 Comment

This does not work: in my case, it returns an array of arrays

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.