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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退