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

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.

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.

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

Thanks!

推荐答案

Update PHP 7.4

Curly brace access syntax is deprecated since PHP 7.4

Update 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.

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

$videos{0}['id']

Php相关问答推荐

PHP中冒号的奇怪用例

将购买限制在WooCommerce中具有相同产品属性的项目

将部分产品排除在WooCommerce的计算附加费之外

如何实现对数据库表中多列的唯一约束?

使注册字段UserMeta值对于WooCommerce中的每个用户是唯一的

如何不重新查询模型A-&>;相关模型B-&>模型

如何在laravel中使用script type= text/html/script

PHP -流加密启用服务器切断字节

Regex|PHP捕获json字符串中的每个非法双引号

防止 WooCommerce 在前端加载全尺寸图像

安装 Herd 并返回 Valet 后为 502

PHP 简单 XML Xpath 上移树(带有名称的父级)

Woocommerce的数量和价格条件

将Shopware 6后端中定义的值导入MultiFilter

htaccess php 文件,然后是文件夹

是否有适用于 Windows 的真正 64 位版本的 PHP 5.6?

在 WooCommerce 结帐后更新用户元字段

基于其他数组创建多维数组 struct

复杂的mysql查询问题

Google Drive - 仅使用 FileID 获取文件的 FileSize