【问题标题】:PHP - refer to stdclass object within array [duplicate]PHP - 引用数组中的stdclass对象[重复]
【发布时间】:2016-08-09 15:15:14
【问题描述】:

上下文

我试图参考下面的价值

Array (
    [0] => stdClass Object (
        [label] => John Smith
        [attributes] => Array (
            [class] => sf-level-0 sf-item-14
        )
        [value] => smith-john
        [depth] => 0
        [count] => 3
    )
    [1] =>... etc.

问题

假设数组名为 $array,那么正确的格式是什么?我试过array[0]->['value'],但它不起作用。

【问题讨论】:

  • $array[0]->value 应该是您需要的。你忘记$了吗?
  • 我有$ - 也刚刚添加到这里。这有效,感谢您的回答和重新格式化!

标签: php arrays stdclass


【解决方案1】:

很难说你的格式。但是:

$array[0]->value

数组中的第一个元素是stdClass Object 类型,因此要访问其成员,请使用对象访问器-> 而不是数组语法。

PS:对 Marc 的编辑表示敬意。

【讨论】:

  • 完美,谢谢!
猜你喜欢
  • 2014-02-28
  • 2016-03-14
  • 2017-05-01
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 2013-05-11
  • 2012-05-28
相关资源
最近更新 更多