dsf6778 2016-09-15 00:17
浏览 34
已采纳

如何从stdClass对象中的数组键获取值[重复]

This question already has an answer here:

I'm building a php webapp and I want to get a list of id's from an array named $array.

stdClass Object
(
  [users] => Array
    (
      [0] => stdClass Object
        (
          [id] => 90379747
          [id_str] => 90379747
        )
      [1] => stdClass Object
        (
          [id] => 30207605
          [id_str] => 30207605        
        )
    )
)

However when I try to get the value from the id key it somehow is not working.

echo "<h1>Result</h1>";

foreach ($array as $obj)
{
  echo $obj->id;
}

What am I doing wrong here. Other examples on stackoverflow seem to suggest this should work.

</div>
  • 写回答

1条回答 默认 最新

  • doubingling4706 2016-09-15 00:37
    关注

    You iterate an object, not an array. Try this way:

    foreach ($array->users as $obj)
    {
      echo $obj->id;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗