按索引号(不是名称)返回 PHP 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3851489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return PHP object by index number (not name)
提问by Ben Guthrie
Goal: retrieve an element of data from within a PHP object by number.
目标:按编号从 PHP 对象中检索数据元素。
This is the print_r($data) of the object:
这是对象的 print_r($data):
stdClass Object
(
[0] => stdClass Object
(
[TheKey] => 1456
[ThingName] => Malibu
[ThingID] => 7037
[MemberOf] => California
[ListID] => 7035
[UserID] => 157
[UserName] => John Doe
)
)
I can't figure out how to pull a value out of it. This is only one record of a multi-record object that should be by id rather than a name.
我不知道如何从中提取价值。这只是多记录对象的一个记录,应该按 id 而不是名称。
These are some failed attempts to illustrate what the goal is:
这些是一些失败的尝试来说明目标是什么:
echo $data -> 0 -> UserName;
echo $data[0] -> UserName;
回答by BoltClock
Normally, PHP variable names can't start with a digit. You can't access $data
as an array either as stdClass
does not implement ArrayAccess
— it's just a normal base class.
通常,PHP 变量名不能以数字开头。您也不能$data
作为数组访问,因为stdClass
它没有实现ArrayAccess
——它只是一个普通的基类。
However, in cases like this you can try accessing the object attribute by its numeric name like so:
但是,在这种情况下,您可以尝试通过数字名称访问对象属性,如下所示:
echo $data->{'0'}->UserName;
The only reason I can think of why Spudley's answer would cause an error is because you're running PHP 4, which doesn't support using foreach
to iterate objects.
我能想到为什么 Spudley 的答案会导致错误的唯一原因是因为您正在运行 PHP 4,它不支持foreach
用于迭代对象。
回答by orrd
BoltClock's suggestion to use "$data->{'0'}->UserName" apparently no longer works with PHP 5.
BoltClock 建议使用 "$data->{'0'}->UserName" 显然不再适用于 PHP 5。
I had the same problem and I found that current() will work to get that numbered class element like this...
我遇到了同样的问题,我发现 current() 可以像这样获得编号的类元素......
echo current($data)->UserName;
Or if that doesn't work (depending on the object) you may need to do another current() call like this:
或者,如果这不起作用(取决于对象),您可能需要执行另一个 current() 调用,如下所示:
echo current(current($data))->UserName;
回答by Pageii Studio
this works for PHP5+
这适用于 PHP5+
echo $data[0]->UserName;
or
或者
foreach ($data as $data){
echo $data->UserName;
}
or as suggested by @orrd
或者按照@orrd 的建议
current($data)->UserName works great too.
回答by Spudley
Have you tried a foreach()
loop? That should give you all the accessible elements, and the
keys it returns may give you a better clue as to how to access them directly.
你试过foreach()
循环吗?这应该为您提供所有可访问的元素,并且它返回的键可能会为您提供有关如何直接访问它们的更好线索。
回答by pixeline
try this:
尝试这个:
echo $data[0]['UserName'];
According to the manual, objects are not meant to be used that way. The comments on this pagedo provide a way around if that's a must-have for you. You could also simply rely on arrays if you are just using the object for properties (and not behaviours).
根据手册,对象不应该以这种方式使用。如果这对您来说是必不可少的,则此页面上的评论确实提供了一种解决方法。如果您只是将对象用于属性(而不是行为),您也可以简单地依赖数组。