Re: [PHP] Inconsistent json_decode() results
On 5 May 2010 22:14, Michael Shadle wrote: > -snip- > > A. 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
Re: [PHP] Inconsistent json_decode() results
On Wed, May 5, 2010 at 1:11 PM, Michiel Sikma 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. A. 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Inconsistent json_decode() results
On 5 May 2010 20:02, Michael Shadle wrote: > 2010/5/5 Michiel Sikma : > > > 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
Re: [PHP] Inconsistent json_decode() results
2010/5/5 Michiel Sikma : > 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) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Inconsistent json_decode() results
2010/5/5 François Beausoleil > 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 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"]}')); Michiel
Re: [PHP] Inconsistent json_decode() results
2010/5/5 Ali Asghar Toraby Parizy > 2010/5/5 François Beausoleil : > > 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 > > I think this is a bug, since you're using it correctly. 5.2.11 seems to have fixed the problem: http://pastie.org/947103 Michiel ps to Ali, most people here seem to prefer it if you type your reply at the bottom of the previous post. I personally don't mind since my client (Gmail) hides quoted text by default, but it does make things easier if you can see the quoted text.
Re: [PHP] Inconsistent json_decode() results
Hi I have this problem too. I think that the version of php results this problem. I have searched later but could not find any solution. 2010/5/5 François Beausoleil : > 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 -- Ali Asghar Torabi -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php