FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » json_decode problem
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: json_decode problem [message #178849 is a reply to message #178847] Sun, 12 August 2012 21:30 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Gregor Kofler wrote:

> Am 2012-08-12 13:44, Thomas 'PointedEars' Lahn meinte:
>> Gregor Kofler wrote:
>>> Am 2012-08-10 23:30, houghi meinte:
>>>> <?php
>>>> $json = file_get_contents("imdb.json");
>>>> $result = json_decode($json);
>>>> foreach($result->data->quotes as $p)
>>>> {
>>>> $qconst = $p->qconst;
>>>> echo "quotenumber = $qconst<br>";
>>>> //Some foreach here?
>>>> //No idea what to put here
>>>> }
>>>> ?>
>>>>
>>>> I do get the above part right, but I am lost on how to do the rest. I
>>>> assume I must do some other foreach.
>>>
>>> foreach($p->lines as $l) {
>>> echo $l->stage;
>>>
>>> if(isset($l->quote)) {
>>> echo $l->quote;
>>> }
>>>
>>> if(isset($l->chars) {
>>> foreach($l->chars as $c) {
>>> echo $c->char;
>>> echo $c->nconst;
>>> }
>>> }
>>>
>>> ....
>>> }
>>
>> FYI: isset() should not be used with properties, because it fails with
>> protected or private properties that have getters:
>
> Thanks, for pointing out potential problems, though the returned object
> is a stdClass instance and all properties are public.

Apparently you have missed (and snipped) the part of my answer that
discusses when and when not to use isset() with json_decode() return values,
and why.

> (Anyway, I prefer to treat JSON responses as associative arrays.)

The second argument passed to json_decode() being TRUE. ACK. However, that
does not help with the isset() issue.


PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom(at)94(dot)75(dot)214(dot)39>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: PEAR Auth package woes
Next Topic: redirect on zend
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 16:47:23 GMT 2024

Total time taken to generate the page: 0.04225 seconds