Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #6773
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| Content-Type | text/plain; charset="UTF-8" |
| Message-ID | <3488766.5KQix4pcSt@PointedEars.de> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <php@PointedEars.de> |
| Organization | PointedEars Software (PES) |
| Date | Sun, 12 Aug 2012 13:44:35 +0200 |
| User-Agent | KNode/4.4.11 |
| Content-Transfer-Encoding | 8Bit |
| X-Face | %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== |
| Subject | Re: json_decode problem |
| Newsgroups | comp.lang.php |
| References | <slrnk2avb8.be8.houghi@penne.houghi> <k055v2$62k$1@dont-email.me> |
| Followup-To | comp.lang.php |
| MIME-Version | 1.0 |
| Lines | 88 |
| NNTP-Posting-Date | 12 Aug 2012 13:44:36 CEST |
| NNTP-Posting-Host | 8675d2d9.newsspool4.arcor-online.net |
| X-Trace | DXC=Vj[K@SFR2L>n`gW2MTm]<34IUK<Cl32<14Fo<]lROoR18kF<OcfhCO;@nETP0LS5I1DZm8W4\YJN<R=i2=[N6Y6:aMk2PL2SD76<S@:giHk?X> |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | csiph.com comp.lang.php:6773 |
Followups directed to: comp.lang.php
Show key headers only | View raw
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:
$ php -r 'class Foo { protected $_bar; public function __construct() {
$this->_bar = 42; } public function __get($name) { return $this->_bar; } }
$x = new Foo(); var_dump($x); var_dump($x->foo); var_dump(isset($x->foo));'
object(Foo)#1 (1) {
["_bar":protected]=>
int(42)
}
int(42)
bool(false)
I have run into this problem recently in an MVC-based commercial application
that I had written, where I was accessing a template variable (implemented
as item of a protected array property of the view class, accessed with
$this->foo). Not using isset() in the template would have saved me hours of
debugging. property_exists() would not work either from public context then
(it would always return FALSE).
isset() usually should also not be used with a json_decode() [0] return
value because JSON allows the `null' literal [1] as value which is parsed
into PHP's NULL value for which isset() returns FALSE [2]:
$ php -r "var_dump(json_decode('{\"foo\": null}'));"
object(stdClass)#1 (1) {
["foo"]=>
NULL
}
$ php -r "\$x = json_decode('{\"foo\": null}'); var_dump(isset(\$x->foo));"
bool(false)
Maybe is_null() or a strict comparison with NULL (whichever you prefer)
serves in general [3]; property_exists() [4] should serve with json_decode()
(where parsed properties are supposed to be public), unless the NULL value
should cause an existing property to be ignored by the program logic as
well.
PointedEars
___________
[0] <http://php.net/json_decode>
[1] <http://json.org/>
[2] <http://php.net/isset>
[3] <http://php.net/is_null>;
<http://www.php.net/manual/en/language.operators.comparison.php>
[4] <http://php.net/property_exists>
--
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-10 23:30 +0200
Re: json_decode problem Gregor Kofler <usenet@gregorkofler.com> - 2012-08-11 10:43 +0200
Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 13:09 +0200
Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-12 13:44 +0200
Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-12 15:28 +0200
Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-12 15:32 +0200
Re: json_decode problem Gregor Kofler <usenet@gregorkofler.com> - 2012-08-12 18:12 +0200
Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-12 23:30 +0200
Re: json_decode problem Curtis Dyer <dyer85@gmail.com> - 2012-08-20 10:41 +0000
Re: json_decode problem Jerry Stuckle <jstucklex@attglobal.net> - 2012-08-20 07:30 -0400
Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-20 16:45 +0200
Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 10:54 +0200
Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-11 11:05 +0200
Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 13:11 +0200
csiph-web