Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #6761 > unrolled thread
| Started by | houghi <houghi@houghi.org.invalid> |
|---|---|
| First post | 2012-08-10 23:30 +0200 |
| Last post | 2012-08-11 13:11 +0200 |
| Articles | 14 — 6 participants |
Back to article view | Back to comp.lang.php
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
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-08-10 23:30 +0200 |
| Subject | json_decode problem |
| Message-ID | <slrnk2avb8.be8.houghi@penne.houghi> |
I am stuck with json_decode. I have a json file
http://houghi.org/Fun/imdb.json
I want it to look like http://www.imdb.com/title/tt0382932/quotes
Well, not completely. Just pure text.
e.g. the second one should be something like
quotenumber = qt0465193
Remy/nm0652663 Stage=observing what Emile is eating Quote=What are you eating?
Emile/nm0812307 Stage=pause Quote=I don't really know. I think it was some sort of wrapper once.
Remy/nm0652663 Quote=What? No! You're in Paris now, baby! My town! No brother of mine eats rejecta-menta in my town!
The code I have is extremely basic. I have been looking at many
websites, but am unable to figure it out what I must put in it to make
it work:
<?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. No idea where to start, even after many days
and websites.
houghi
--
You can have my keyboard ...
if you can pry it from my dead, cold, stiff fingers
[toc] | [next] | [standalone]
| From | Gregor Kofler <usenet@gregorkofler.com> |
|---|---|
| Date | 2012-08-11 10:43 +0200 |
| Message-ID | <k055v2$62k$1@dont-email.me> |
| In reply to | #6761 |
Am 2012-08-10 23:30, houghi meinte:
> I am stuck with json_decode. I have a json file
> http://houghi.org/Fun/imdb.json
> I want it to look like http://www.imdb.com/title/tt0382932/quotes
> Well, not completely. Just pure text.
>
> e.g. the second one should be something like
> quotenumber = qt0465193
> Remy/nm0652663 Stage=observing what Emile is eating Quote=What are you eating?
> Emile/nm0812307 Stage=pause Quote=I don't really know. I think it was some sort of wrapper once.
> Remy/nm0652663 Quote=What? No! You're in Paris now, baby! My town! No brother of mine eats rejecta-menta in my town!
>
> The code I have is extremely basic. I have been looking at many
> websites, but am unable to figure it out what I must put in it to make
> it work:
>
> <?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;
}
}
....
}
> No idea where to start, even after many days
> and websites.
And never heard of var_dump()?
Gregor
--
http://vxweb.net
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-08-11 13:09 +0200 |
| Message-ID | <slrnk2cfc0.4f7.houghi@penne.houghi> |
| In reply to | #6767 |
Gregor Kofler wrote:
>> 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;
> }
> }
>
> ....
> }
Thanks.
>
>> No idea where to start, even after many days
>> and websites.
>
> And never heard of var_dump()?
Yes and I used it, but unfortunatly the outcome is meaningless to me.
Searching google is also very hard if you have no idea what you should
enter in the search. :-(
The sites I found that explained with examples (as that is the best way
I can learn) did not go into enough detail and were very basic.
houghi
--
This is written under the inluence of the following:
> Artist : James Brown
> Song : Instrumental Bridge 2
> Album : Live At The Apollo
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-08-12 13:44 +0200 |
| Message-ID | <3488766.5KQix4pcSt@PointedEars.de> |
| In reply to | #6767 |
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
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2012-08-12 15:28 +0200 |
| Message-ID | <olnkf9-hal.ln1@luuk.invalid.lan> |
| In reply to | #6773 |
On 12-08-2012 13:44, Thomas 'PointedEars' Lahn wrote:
> $ php -r "\$x = json_decode('{\"foo\": null}'); var_dump(isset(\$x->foo));"
> bool(false)
php -r "\$y = null; \$x = json_decode('{\"foo\": null}');
var_dump(isset(\$y));"
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2012-08-12 15:32 +0200 |
| Message-ID | <9tnkf9-fcl.ln1@luuk.invalid.lan> |
| In reply to | #6773 |
On 12-08-2012 13:44, Thomas 'PointedEars' Lahn wrote:
> 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:
>
....
....
>
maybe you should not use
var_dump(isset(...));
it's pretty useless because:
isset() will always return 'true' of 'false'
[toc] | [prev] | [next] | [standalone]
| From | Gregor Kofler <usenet@gregorkofler.com> |
|---|---|
| Date | 2012-08-12 18:12 +0200 |
| Message-ID | <k08klq$d1v$1@dont-email.me> |
| In reply to | #6773 |
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.
(Anyway, I prefer to treat JSON responses as associative arrays.)
Gregor
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-08-12 23:30 +0200 |
| Message-ID | <20380527.UJKVNz4fzF@PointedEars.de> |
| In reply to | #6776 |
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@94.75.214.39>
[toc] | [prev] | [next] | [standalone]
| From | Curtis Dyer <dyer85@gmail.com> |
|---|---|
| Date | 2012-08-20 10:41 +0000 |
| Message-ID | <k0t48i$8mk$1@dont-email.me> |
| In reply to | #6773 |
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
> Gregor Kofler wrote:
>
>> Am 2012-08-10 23:30, houghi meinte:
<snip>
>> 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).
According to the PHP documentation, as of PHP 5.3.0,
`property_exists()' will also acknowledge protected and private
properties. [1]
When using at least PHP 5.1.0, one might also consider PHP
reflection to check for private and protected properties:
<?php
class Foo {
private $test;
}
$r = new ReflectionObject(new Foo());
var_dump($r->hasProperty('test')); /* bool(true) */
?>
<snip>
______
[1] <http://php.net/property_exists>
--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2012-08-20 07:30 -0400 |
| Message-ID | <k0t75b$j41$3@dont-email.me> |
| In reply to | #6817 |
On 8/20/2012 6:41 AM, Curtis Dyer wrote:
> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
>
>> Gregor Kofler wrote:
>>
>>> Am 2012-08-10 23:30, houghi meinte:
>
> <snip>
>
>>> 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).
>
> According to the PHP documentation, as of PHP 5.3.0,
> `property_exists()' will also acknowledge protected and private
> properties. [1]
>
> When using at least PHP 5.1.0, one might also consider PHP
> reflection to check for private and protected properties:
>
> <?php
>
> class Foo {
> private $test;
> }
>
> $r = new ReflectionObject(new Foo());
> var_dump($r->hasProperty('test')); /* bool(true) */
>
> ?>
>
>
> <snip>
>
> ______
> [1] <http://php.net/property_exists>
>
You should not need either. Properties are private or protected for a
reason!
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-08-20 16:45 +0200 |
| Message-ID | <7187320.lErI1fH90z@PointedEars.de> |
| In reply to | #6817 |
Curtis Dyer wrote:
> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
>> 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).
>
> According to the PHP documentation, as of PHP 5.3.0,
> `property_exists()' will also acknowledge protected and private
> properties. [1]
ACK:
$ php -r 'class Foo { private $bar; } var_dump(property_exists(new Foo(),
"bar"));'
bool(true)
$ php -r 'class Foo { private $bar; } var_dump(property_exists(new Foo(),
"baz"));'
bool(false)
$ php -r 'class Foo { protected $bar; } var_dump(property_exists(new Foo(),
"bar"));'
bool(true)
$ php -r 'class Foo { protected $bar; } var_dump(property_exists(new Foo(),
"baz"));'
bool(false)
$ php -v
PHP 5.3.10-1 (cli) (built: Feb 3 2012 10:03:01)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
with XCache v1.3.2, Copyright (c) 2005-2011, by mOo
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
One could consider that an OOP bug. On the other hand, var_dump() also
dumps private and protected properties from public context:
$ php -r 'class Foo { private $bar; } var_dump(new Foo());'object(Foo)#1 (1)
{
["bar":"Foo":private]=>
NULL
}
$ php -r 'class Foo { protected $bar; } var_dump(new Foo());'
object(Foo)#1 (1) {
["bar":protected]=>
NULL
}
Either both functions are OOP-buggy, or none is.
> When using at least PHP 5.1.0, one might also consider PHP
> reflection to check for private and protected properties:
>
> <?php
>
> class Foo {
> private $test;
> }
>
> $r = new ReflectionObject(new Foo());
> var_dump($r->hasProperty('test')); /* bool(true) */
>
> ?>
Overkill in most cases.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-08-11 10:54 +0200 |
| Message-ID | <slrnk2c7dk.4f7.houghi@penne.houghi> |
| In reply to | #6761 |
houghi wrote:
> I am stuck with json_decode. I have a json file
> http://houghi.org/Fun/imdb.json
> <?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
> }
> ?>
As always one finds the answer shortly after posting, regardless how
many hours you were looking for the solution. (30-40 over a weeks
period)
The file I use is http://houghi.org/Fun/imdb.json
<?php
$json = file_get_contents("imdb.json");
$result = json_decode($json);
foreach($result->data->quotes as $p)
{
$qconst = $p->qconst;
print "quote=$qconst<br>\n";
$qconst = $p->lines;
foreach ($qconst as $q)
{
$quote = $q->quote;
$stage = $q->stage;
if ($stage != "") { $stage = '<i>['.$stage.']</i> ';}
$chars = $q->chars;
foreach ($q->chars as $r)
{
$char = $r->char." ";
$nconst = $r->nconst." ";
}
print $char.$nconst.$stage.$quote."<br>\n";
}
print "<hr>\n";
}
?>
It seems obvious now. Anybody an idea on how to improve it? I myself
will re-write the output to put the data it in a database (for personal
use).
houghi
--
You can have my keyboard ...
if you can pry it from my dead, cold, stiff fingers
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2012-08-11 11:05 +0200 |
| Message-ID | <5rjhf9-7au.ln1@luuk.invalid.lan> |
| In reply to | #6768 |
On 11-08-2012 10:54, houghi wrote:
> houghi wrote:
>> I am stuck with json_decode. I have a json file
>> http://houghi.org/Fun/imdb.json
>> <?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
>> }
>> ?>
>
> As always one finds the answer shortly after posting, regardless how
> many hours you were looking for the solution. (30-40 over a weeks
> period)
>
> The file I use is http://houghi.org/Fun/imdb.json
>
> <?php
> $json = file_get_contents("imdb.json");
> $result = json_decode($json);
> foreach($result->data->quotes as $p)
> {
> $qconst = $p->qconst;
> print "quote=$qconst<br>\n";
> $qconst = $p->lines;
> foreach ($qconst as $q)
> {
> $quote = $q->quote;
> $stage = $q->stage;
> if ($stage != "") { $stage = '<i>['.$stage.']</i> ';}
> $chars = $q->chars;
> foreach ($q->chars as $r)
> {
> $char = $r->char." ";
> $nconst = $r->nconst." ";
> }
> print $char.$nconst.$stage.$quote."<br>\n";
> }
> print "<hr>\n";
> }
> ?>
>
> It seems obvious now. Anybody an idea on how to improve it? I myself
> will re-write the output to put the data it in a database (for personal
> use).
>
> houghi
>
use isset() to check if a quote of stage exists....
(see post from Gregor)
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-08-11 13:11 +0200 |
| Message-ID | <slrnk2cfei.4f7.houghi@penne.houghi> |
| In reply to | #6770 |
Luuk wrote: > use isset() to check if a quote of stage exists.... > (see post from Gregor) Indeed. Stoopid me. Thanks to pointing it out to me. houghi -- This is written under the inluence of the following: > Artist : Guano Apes > Song : Move A Little Closer > Album : Guano Apes live
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web