dousui6193 2015-03-06 01:58
浏览 29
已采纳

使用带有1d和2d数组的php json_decode

I am using json_decode in PHP to decode a JSON object which may have 1d array or 2d array for a value:

{"Cell":{"@column":"ZjE6dW5pdmVyc2l0eQ==","@timestamp":"1425598820484","$":"MC44MDc2NDEwNDg0MjI5MjMy"}}

or

{"Cell":[{"@column":"ZjE6YQ==","@timestamp":"1425599309809","$":"MC4wNTYzMzgwMjgxNjkwMTQwODY="},{"@column":"ZjE6YW5k","@timestamp":"1425599309809","$":"MC4wNTYzMzgwMjgxNjkwMTQwODY="},{"@column":"ZjE6Y2F0Y2hlcw==","@timestamp":"1425599309809","$":"MC4wNDIyNTM1MjExMjY3NjA1Ng=="},{"@column":"ZjE6aQ==","@timestamp":"1425599309809","$":"MC4wOTg1OTE1NDkyOTU3NzQ2NA=="},{"@column":"ZjE6dGhhdA==","@timestamp":"1425599309809","$":"MC4xNjkwMTQwODQ1MDcwNDIyNQ=="}]}

and I am using $Cell = $json["Cell"] to access the elements. The problem I am facing is that the second case plays nicely and I get an array of arrays while the first one should be a single element array but is being interpreted as a 3 element array.

  • 写回答

1条回答 默认 最新

  • drz5553 2015-03-06 02:16
    关注

    Can't you just check if the element has the key? Like this:

    if (isset($json['Cell']['@column']) {
        // do stuff with single-element
    } else {
        // it is a collection
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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