Re: RFC: Short syntax for Arrays (redux)
| From: | Matt Wilson | Date: | Tue, 31 May 2011 22:02:59 +0000 |
| Subject: | Re: RFC: Short syntax for Arrays (redux) | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-52601@lists.php.net to get a copy of this message | ||
Exactly my point. Why try to fudge json into php, instead of just improving php?
On May 31, 2011, at 5:02 PM, Dan Birken wrote:
> Yes it does :)
>
> I guess my point was it would be confusing if PHP supported JSON-like
> syntax, but json_decode(X) was different from eval(X). So if PHP isn't
> going to use JSON syntax (because it doesn't work for assoc arrays), then I
> don't think there is much benefit in using *almost* JSON syntax.
>
> It just seems simpler that:
> array(1, 2) equals [1, 2]
> array(1 => 2) equals [1 => 2]
>
> -Dan
>
> On Tue, May 31, 2011 at 2:42 PM, Rasmus <rasmus@lerdorf.com> wrote:
>
>> On 05/31/2011 02:34 PM, Dan Birken wrote:
>>> It is worth noting that point you bring up is one that is present in PHP
>>> currently:
>>>
>>> php > print_r(json_decode(json_encode(array('a' => 'b'))));
>>> stdClass Object
>>> (
>>> [a] => b
>>> )
>>>
>>> php > print_r(json_decode(json_encode(array('a', 'b'))));
>>> Array
>>> (
>>> [0] => a
>>> [1] => b
>>> )
>>
>> Sure, but that makes makes perfect sense, does it not? Javascript does
>> not have associative arrays. So any non-scalar array has to be mapped to
>> a Javascript object in order to be represented. When we bring it back we
>> map a Javascript object to a PHP object, however, there is an optional
>> argument on json_decode() to change that to an associative array if
>> desired.
>>
>> The point we are making is that this exact inability of the json
>> notation to distinguish an associative array from an object makes it a
>> problematic choice for a native syntax in PHP.
>>
>> -Rasmus
>>