From: =?iso-8859-1?Q?Fran=E7ois_Beausoleil?= on
Hello all!

We're seeing inconsistent json decoding between PHP versions: http://gist.github.com/390090

Calling json_decode() from 5.2.6 returns an associative array when asked, while 5.2.10 always returns a stdClass. Is this a bug or a problem with my calling convention?

Thanks!
François
From: Michael Shadle on
2010/5/5 Michiel Sikma <michiel(a)thingmajig.org>:

> By the way, if you're stuck on 5.2.10, you could simply cast the result to
> array:
>
> var_dump((array)json_decode('{"_urls": ["a", "b"]}'));

I don't see a "available starting in 5.x.x" notice, so I think it's
been there for a long time...

http://www.php.net/json_decode

Second parameter of true makes it return an array instead of a class
(I just had to use this recently, that's why it popped in my mind)
From: Michiel Sikma on
On 5 May 2010 20:02, Michael Shadle <mike503(a)gmail.com> wrote:

> 2010/5/5 Michiel Sikma <michiel(a)thingmajig.org>:
>
> > By the way, if you're stuck on 5.2.10, you could simply cast the result
> to
> > array:
> >
> > var_dump((array)json_decode('{"_urls": ["a", "b"]}'));
>
> I don't see a "available starting in 5.x.x" notice, so I think it's
> been there for a long time...
>
> http://www.php.net/json_decode
>

You're right, but this is about how 5.2.10 ignores the second parameter and
always returns a class, which appears to be a bug. I'm not sure which other
versions have this same problem, but 5.2.11 has correct behavior, which
seems to suggest they found and fixed it by then. I can't find an entry for
this problem in PHP's bug database, however.

My suggestion to typecast the result of json_decode() should only be
followed if you need an array and are required to work with version 5.2.10.

Michiel
From: Michael Shadle on
On Wed, May 5, 2010 at 1:11 PM, Michiel Sikma <michiel(a)thingmajig.org> wrote:

> You're right, but this is about how 5.2.10 ignores the second parameter and
> always returns a class, which appears to be a bug. I'm not sure which other
> versions have this same problem, but 5.2.11 has correct behavior, which
> seems to suggest they found and fixed it by then. I can't find an entry for
> this problem in PHP's bug database, however.
> My suggestion to typecast the result of json_decode() should only be
> followed if you need an array and are required to work with version 5.2.10.

Ahhhh. I gotcha now. Yeah I started using that just recently either
5.2.11 or 5.2.13 (not sure which)

You can typecast an object to an array that easily? I was unaware.
From: Michiel Sikma on
On 5 May 2010 22:14, Michael Shadle <mike503(a)gmail.com> wrote:

> -snip-
>
> Ahhhh. I gotcha now. Yeah I started using that just recently either
> 5.2.11 or 5.2.13 (not sure which)
>
> You can typecast an object to an array that easily? I was unaware.
>


Yep, it's that easy. :-)

// array(1) {
// [0]=>
// int(1)
// }
var_dump((array)1);

// array(1) {
// [0]=>
// string(1) "a"
// }
var_dump((array)'a');

// array(1) {
// ["c"]=>
// string(1) "d"
// }
class b {
public $c = 'd';
}
var_dump((array)new b());

Michiel