Search code examples
phpsymfony4

Turn object to an array and get its values


foreach ($this->getDoctrine()->getRepository(Personal::class)->findBy(array('uid' => $id)) as $object) {

In my code $object returns an object with the values and columns from my database. I would like to convert that object to an array. I can do this with $array = (array) $object;.

If I use var_export($array), this will be the output:

array (
  '' . "\0" . 'App\\Entity\\Personal' . "\0" . 'id' => 5,
  '' . "\0" . 'App\\Entity\\Personal' . "\0" . 'uid' => 43,
  '' . "\0" . 'App\\Entity\\Personal' . "\0" . 'city' => 'Zurich',
)

I would like to get the value of city with $array['city'] but "city" is an undefined index. What's the best way to get the value?


Solution

  • Instead of using findBy, write a doctrine custom query then set the result as Array. As here : Symfony 4, doctrine, getResult and getArrayResult and getScalarResult return same structure results