【问题标题】:Php extract objects vars without cycling object itselfPHP 提取对象 vars 而不循环对象本身
【发布时间】:2011-09-13 12:00:26
【问题描述】:

嗨,我有这个对象:

object(stdClass)#27 (1) {
 [0]=> object(stdClass)#26 (6){
 ["_id"]=> object(MongoId)#24 (1) {
 ["$id"]=> string(24) "4e6ea439caa47c2c0c000000" 
}
["username"]=> string(16) "wdfkewkghbrjkghb" ["email"]=> string(24) "wdhbfjkwhegerg@€rg.efg" ["password"]=> string(32) "4297f44b13955235245b2497399d7a93" ["slug"]=> string(16) "wdfkewkghbrjkghb" ["insert_datetime"]=> string(19) "2011-09-13 12:09:49" 
} 

} 

如何在不循环对象的情况下检索例如对象->用户名?

谢谢

[编辑]

这是我的一小段代码:

$user = $this->model_user->populateBy($id = '12');
            var_dump($user);
            echo $user->username ; // error:Trying to get property of non-object  non object

正在做 var_dump($user);它打印出我粘贴的对象。

【问题讨论】:

  • “循环”对象是什么意思? $object->username 不工作吗?
  • “循环对象”是什么意思?你能展示你正在使用的 PHP 代码吗?
  • 我想你需要看看 PHP 中的 OOP 参考
  • 这个对象从我分配给 $user = $query_result = $object; 的查询返回那么如果我回显 $user->username;它说错误:试图获取非对象的属性
  • while , foreach( $user as $row) { echo $row->username; } 作品

标签: php arrays object cycle extraction


【解决方案1】:

我认为get_object_vars() 是您所需要的。

我不确定我是否理解这个问题;如果你想要用户名,你可以做$Object->username..

【讨论】:

  • get_object_vars() 返回空数组 :(
猜你喜欢
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2020-09-12
  • 1970-01-01
相关资源
最近更新 更多