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> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn 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+&. 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 X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.php:6773 Gregor Kofler wrote: > Am 2012-08-10 23:30, houghi meinte: >> > $json = file_get_contents("imdb.json"); >> $result = json_decode($json); >> foreach($result->data->quotes as $p) >> { >> $qconst = $p->qconst; >> echo "quotenumber = $qconst
"; >> //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] [1] [2] [3] ; [4] -- > 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