将对象转换为数组并获取其值

Turn object to an array and get its values

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

在我的代码中 $object returns 一个包含来自我的数据库的值和列的对象。我想将该对象转换为数组。我可以用 $array = (array) $object;.

做到这一点

如果我使用 var_export($array),这将是输出:

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

我想用 $array['city'] 获取 city 的值,但 "city" 是一个未定义的索引。获得价值的最佳方式是什么?

不使用 findBy,而是编写一个学说自定义查询,然后将结果设置为数组。如此处:Symfony 4, doctrine, getResult and getArrayResult and getScalarResult return same structure results