double0201 2018-03-26 14:39
浏览 41
已采纳

从assoc数组中获取值,返回oe?

I've got an array (provided by an api), of which I try to get the ID form an establishment. I put the result in a $decodeArray = json_decode($result); and that I put that result in a var_dump(); Which returns:

object(stdClass)#1 (2) {
  ["ok"]=>
  bool(true)
  ["establishments"]=>
  array(4) {
    [0]=>
    object(stdClass)#2 (2) {
      ["level"]=>
      string(5) "admin"
      ["establishment"]=>
      object(stdClass)#3 (5) {
        ["id"]=>
        string(36) "0b246874-2c53-11e8-8fb6-001a4aa8ea16"
        ["name"]=>
        string(9) "robinsons"
        ["address"]=>
        string(24) "Belfast northern ireland"
        ["lat"]=>
        float(54.594869)
        ["lng"]=>
        float(-5.933954)
      }
    }
    [1]=>
    object(stdClass)#4 (2) {
      ["level"]=>
      string(5) "admin"
      ["establishment"]=>
      object(stdClass)#5 (5) {
        ["id"]=>
        string(36) "1e95af3a-2ce7-11e8-8fb6-001a4aa8ea16"
        ["name"]=>
        string(7) "reimink"
        ["address"]=>
        string(13) "dorpsstraat 6"
        ["lat"]=>
        float(123.123)
        ["lng"]=>
        float(-74.99)
      }
    }
    [2]=>
    object(stdClass)#6 (2) {
      ["level"]=>
      string(5) "admin"
      ["establishment"]=>
      object(stdClass)#7 (5) {
        ["id"]=>
        string(36) "3d1e500b-2c4d-11e8-8fb6-001a4aa8ea16"
        ["name"]=>
        string(14) "kelly's cellar"
        ["address"]=>
        string(24) "Belfast northern ireland"
        ["lat"]=>
        float(54.599508)
        ["lng"]=>
        float(-5.93216)
      }
    }
    [3]=>
    object(stdClass)#8 (2) {
      ["level"]=>
      string(5) "admin"
      ["establishment"]=>
      object(stdClass)#9 (5) {
        ["id"]=>
        string(36) "75987f56-2de4-11e8-8fb6-001a4aa8ea16"
        ["name"]=>
        string(7) "reimink"
        ["address"]=>
        string(12) "graslanden 2"
        ["lat"]=>
        float(3456.33)
        ["lng"]=>
        int(-1)
      }
    }
  }
}

I try to echo all the items like the 'id' and 'name' from every establishment. I tried reaching the values, but can't quite seem to get it right.

 foreach ($decodeArray as $item => $key)
 { 
 echo $item['establishments'][0]['establishment']['name'];
 }

The above foreach loop seems to be the closest I can get, but it only returns "oe" (no clue where that originates from).

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dongya9904 2018-03-26 14:50
    关注

    You need to pass true to json_decode to get an array, but then you need to look at the nesting:

    foreach ($decodeArray['establishments'] as $values)
    { 
        echo $values['establishment']['name'];
    }
    

    To do it with the objects you have it would be:

    foreach ($decodeArray->establishments as $values)
    { 
        echo $values->establishment->name;
    }
    

    The o and e come from the first letter of the only two top level properties: "ok" and "establishments". Because of the way you're trying to access them the 0 is treated as a string index and you are operating on the property name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料