1

If I have a array formatted as below

[
    {
        'id':1,
        'some_int': 3,
        'foo':'bar'
    }
]

How can I retrieve, for example, 'bar' value?

If I write

$array[0]['bar']

It throws Cannot use object of type stdClass as array

If I write

$array['bar']

It throws Unidentified index: bar

Jorge Anzola
  • 1,165
  • 3
  • 13
  • 33
  • 2
    When you do your `json_decode` make sure the second param is set to `true`. This will decode it all as array. Without it, `$array[0]` is and object of type `stdClass` which you'd need to access as `$array[0]->bar` depending on PHP version. – Jonnix Oct 29 '16 at 12:28
  • Short answer: what you have, is not an array. Not in PHP-terms, at least. – junkfoodjunkie Oct 29 '16 at 12:29
  • its an json array first convert it into php array by json_encode($json_array) and the get value by foreach loop – Hassan Tahir Oct 29 '16 at 12:33
  • @HassanTahir 1) If you have JSON, you need to json_decode, not encode. 2), going bt the error messages I think we can be confident OP has already decoded. – Jonnix Oct 29 '16 at 12:37
  • sorry i wrote it mistakenly its an json array first convert it into php array by json_decode($json_array) and the get value by foreach loop – Hassan Tahir Oct 29 '16 at 12:40

0 Answers0