【问题标题】:Get fields from object从对象中获取字段
【发布时间】:2016-10-20 16:01:50
【问题描述】:

我收集了 Country 字段:name、aaa、bbb、ccc、ddd。

例子:

{_id: xxx1, name: "USA", aaa: "daf", ccc: "sdfgsdf", ddd: "234"}
{_id: xxx2, name: "Mexico", aaa: "dfg", bbb: "fdsgsdf"}
{_id: xxx3, name: "Germany", bbb: "ddfgaf"}
{_id: xxx4, name: "France"}

在我的 Symfony 应用程序的控制器中,我将所有对象获取到变量 $countries 和下一个:

foreach ($countries as $country) {
    //how can I get here fields for current object? 
    //For _id: xxx1 I would like receive "_id, name, aaa, ccc, ddd" and for _id: xxx4 I would like receive "_id, name"
}

【问题讨论】:

  • 您有什么想问的吗?
  • 它似乎作为注释嵌入在代码中。

标签: php mongodb symfony doctrine


【解决方案1】:

您可以在 foreach 中使用 get_object_vars() 和 array_keys()。

foreach ($countries as $country) {
   $fields = array_keys(get_object_vars($country)); 
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-31
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多