drpiqlzrh62917192 2014-03-28 03:21
浏览 33
已采纳

从JSON_DECODE移动2D数组

I cURL to a page which produces an array of json of the fashion:

[{"id":1,"name":"Drew"},{"id":2,"name":"Joyce"},{"id":3,"name":"Vick"}]

I use $result = json_decode($json, true) to get back an associative array.

I am having trouble though iterating through the array though and accessing individual pieces of each json object like the name field. Is there an easy way to accomplish this?

Another issue is, I was planning to use a foreach loop as I might not know the exact length of the array of json objects, is there a way to incorporate the solution to my first issue into a for each loop or other structure that might take in a variable sized array and stop when it reaches the end?

  • 写回答

1条回答 默认 最新

  • duansaxf095988 2014-03-28 03:37
    关注

    This should be fairly simple...

    foreach ($result as $item) {
        echo 'id: ', $item['id'],
             ', name: ', $item['name'],
             PHP_EOL;
    }
    

    Demo here - http://ideone.com/xwy2zH

    Whilst developing, I also highly recommend you enable full error reporting. Set these properties in your development environment's php.ini file then restart your web server

    error_reporting = E_ALL
    display_errors = On
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?