dongshengli6384
dongshengli6384
2015-11-12 23:31

无法访问PHP数组的成员[关闭]

已采纳

enter image description here

This JSON I have converted to an array in PHP. when i get the sizeof or count, the array size is 1 Do you not get an associative array using json_decode in PHP? after the JSON below you see i echo out the typeof, so you see it is array, then size of, and you see it is 1 Any idea why I cant access the value for 'name' in the resulting array and why it is of size 1?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duandai7601 duandai7601 6年前

    The reason count is returning 1 is because your JSON is structured in such a way that it represents an array of objects, of which you only have 1 (note the single set of braces). If you would like to count all elements within the first and only object in your JSON array, you could instead try:

    count($json[0]);
    
    点赞 评论 复制链接分享
  • duanjiagu0655 duanjiagu0655 6年前

    Well, look at the JSON. It's an array of an object.

    echo $json_decoded[0]->name; // chemist

    var_dump may be useful next time.

    点赞 评论 复制链接分享