获取 PHP stdObject 中的第一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9474446/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:54:25  来源:igfitidea点击:

Get first element in PHP stdObject

phpobjectmultidimensional-array

提问by Drew Baker

I have an object (stored as $videos) that looks like this

我有一个看起来像这样的对象(存储为 $videos)

object(stdClass)#19 (3) {
  [0]=>
  object(stdClass)#20 (22) {
    ["id"]=>
    string(1) "123"

  etc...

I want to get the ID of just that first element, without having to loop over it.

我只想获取第一个元素的 ID,而不必遍历它。

If it were an array, I would do this:

如果它是一个数组,我会这样做:

$videos[0]['id']

It used to work as this:

它曾经是这样工作的:

$videos[0]->id

But now I get an error "Cannot use object of type stdClass as array..." on the line shown above. Possibly due to a PHP upgrade.

但是现在我在上面显示的行上收到错误“无法使用 stdClass 类型的对象作为数组...”。可能是由于 PHP 升级。

So how do I get to that first ID without looping? Is it possible?

那么如何在不循环的情况下获得第一个 ID?是否可以?

Thanks!

谢谢!

回答by MrTrick

Both array() and the stdClass objects can be accessed using the current()key()next()prev()reset()end()functions.

array() 和 stdClass 对象都可以使用这些 函数访问 。current()key()next()prev()reset()end()

So, if your object looks like

所以,如果你的对象看起来像

object(stdClass)#19 (3) {
  [0]=>
  object(stdClass)#20 (22) {
    ["id"]=>
    string(1) "123"
  etc...

Then you can just do;

然后你就可以了;

$id = reset($obj)->id; //Gets the 'id' attr of the first entry in the object

If you need the key for some reason, you can do;

如果您出于某种原因需要密钥,您可以这样做;

reset($obj); //Ensure that we're at the first element
$key = key($obj);

Hope that works for you. :-) No errors, even in super-strict mode, on PHP 5.4

希望这对你有用。:-) 在 PHP 5.4 上没有错误,即使在超级严格模式下

回答by Clain Dsilva

Update 2019

2019 年更新

Moving on to the best practices of OOPS, @MrTrick's answer must be marked as correct, although my answer provides a hacked solution its not the best method.

转到 OOPS 的最佳实践,@MrTrick 的答案必须标记为正确,尽管我的答案提供了一个破解解决方案,但它不是最佳方法。

Simply iterate its using {}

只需使用 {} 对其进行迭代

Example:

例子:

$videos{0}->id

This way your object is not destroyed and you can easily iterate through object.

这样您的对象就不会被破坏,您可以轻松地遍历对象。

For PHP 5.6 and below use this

对于 PHP 5.6 及以下,请使用此

$videos{0}['id']

回答by froilanq

Correct:

正确的:

$videos= (Array)$videos;
$video = $videos[0];

回答by Patrick

much easier:

容易得多:

$firstProp = current( (Array)$object );

回答by Biraj Bora

$videos->{0}->idworked for me.

$videos->{0}->id为我工作。

Since $videos and {0} both are objects, so we have to access id with $videos->{0}->id. The curly braces are required around 0, as omitting the braces will produce a syntax error : unexpected '0', expecting identifier or variable or '{' or '$'.

由于 $videos 和 {0} 都是对象,所以我们必须使用 $videos->{0}->id. 大括号需要在 0 左右,因为省略大括号会产生语法错误:意外的 '0',需要标识符或变量或 '{' 或 '$'。

I'm using PHP 5.4.3.

我正在使用PHP 5.4.3

In my case, neither $videos{0}->idand $videos{0}['id']worked and shows error :

在我的情况下,既没有$videos{0}->id$videos{0}['id']合作,并显示错误:

Cannot use object of type stdClass as array.

不能使用 stdClass 类型的对象作为数组。

回答by Rolf

You could loop on the object maybe and break in the first loop... Something like

你可以在对象上循环,然后在第一个循环中中断......就像

foreach($obj as $prop) {
   $first_prop = $prop;
   break; // exits the foreach loop
} 

回答by Thomas Decaux

Playing with Php interactive shell, Php 7:

玩 PHP 交互式 shell,Php 7:

?  ~ php -a
Interactive shell

php > $v = (object) ["toto" => "hello"];
php > var_dump($v);
object(stdClass)#1 (1) {
  ["toto"]=>
  string(5) "hello"
}
php > echo $v{0};
PHP Warning:  Uncaught Error: Cannot use object of type stdClass as array in php shell code:1
Stack trace:
#0 {main}
  thrown in php shell code on line 1

Warning: Uncaught Error: Cannot use object of type stdClass as array in php shell code:1
Stack trace:
#0 {main}
  thrown in php shell code on line 1
php > echo $v->{0};
PHP Notice:  Undefined property: stdClass::##代码## in php shell code on line 1

Notice: Undefined property: stdClass::##代码## in php shell code on line 1
php > echo current($v);
hello

Only currentis working with object.

current与对象一起工作。