php - 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)
解决方案
也许
$reflection = new \ReflectionClass($object);
$property = $reflection->getProperty(0);
或者
$reflection = new \ReflectionClass($object);
$property = $reflection->getProperty('0');
推荐阅读
- swift - 提交到 AppStore 时,通过 Xcode 中的 UIWebView.loadRequest() 加载 https:// 网站是否算作加密?
- php - 如果我 print_r 对象,“尝试在 null 上读取属性”会消失
- sql - 如何为每个用户获取第一个订阅(假设订阅 ID 每次自动更新时都会更改)
- ios - Invariant Violation: new NativeEventEmitter() 需要在 React Native 中使用蓝牙的非空参数
- c# - 如何控制从旋转对象的中心到另一个对象的半径?
- python - 当满足另一列的条件时,如何使用 pandas diff() 函数重置计数?
- lua - 如何使表格中的数字成为lua中无法解决的字符串?
- python - 为什么这个 doctest 在 Pycharm 中通过,而不是在命令行中?
- java - 警告:[unchecked] 作为原始类型 Comparable 的成员对 compareTo(T) 的未经检查的调用
- python - 比较两个不同长度的 df,收到“只能比较标记相同的系列对象”错误