Bart de Boer wrote:
Mike Naberezny wrote:
Bart de Boer wrote:
Maybe introduce an optional second argument "decodetype" to
json_decode() where you could pass on a constant like JSON_ARRAY or
JSON_OBJECT?
For example:
$assoc_array = json_decode($json_string, JSON_ARRAY);
$object = json_decode($json_string, JSON_OBJECT);
I'm not saying this is a bad idea but is it really necessary? It
seems to just add extra complexity. If json_decode() returns an
associative array then you can cast easily enough:
$obj = (object)json_decode($jsonSerialized);
Mike
That's not entirely true since the properties of the resulting object
would then still be made out of associative arrays. (in stead of objects)
I'm not saying it's necessary to have it return objects. I'm leaning
towards associative arrays myself too. On the other hand, objects seem
to be more consistent with the behaviour in Javascript. The specs seems
to allow both. Hence my suggestion.
Bart
I was wrong when I said that objects would be more consistent with the
behaviour in JavaScript. When I said that I had the following JavaScript
syntax in mind:
object.property;
of which the PHP equivalent would be:
$object->property;
But then Douglas Crockford got me thinking. And after snooping around at
crockford.com I (re)discovered that objects can also be accessed like:
object['property'];
Which would be consistent with:
$object['property'];
So, making a long story short: And FWIW: I'm all +1 for implementing it
as associative arrays. :)
Bart
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php