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.
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.