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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化