首页 > 解决方案 > PHP从键为整数的对象中获取键

问题描述

我在 PHP 中有一个对象

object(SimpleXMLElement)[974]
   public '@attributes' => 
     array (size=1)
     'index' => string 'vDEF' (length=4)
   public 0 => string 'Link Title' (length=10)

如何返回值 public 0 ?

我试过了

 $obj[0]
 $obj->0
 $obj->[0]

没有一个给我结果..

使用 PHP 5.7

更新代码以提供数组

array (size=2)
   '@attributes' => 
   array (size=1)
      'index' => string 'vDEF' (length=4)
      0 => string 'Link Title' (length=10)

标签: phpobject

解决方案


也许

$reflection = new \ReflectionClass($object); $property = $reflection->getProperty(0);

或者

$reflection = new \ReflectionClass($object); $property = $reflection->getProperty('0');


推荐阅读