Why is it possible to access array indexes with "{}" keys?

4

In some tests I did with PHP, I saw that it is possible to use keys ( {} ) to access indexes of arrays .

Example:

$array = ['a' => 1, 'b' => 2, 'c' => ['d' => 4]]

echo $array{'c'}{'d'}; // 4
echo $array{'c'}['d']; // 4

Until then, I thought that only the brackets ( [] ) did that.

So, I want to ask:

  • Accessing an index of a array with braces instead of brackets has any special significance?

  • Why is it possible to use both forms?

  • Using keys would be considered non-standard? I ask this because, although it works, I've never seen anyone using it.

asked by anonymous 04.01.2017 / 20:37

1 answer

4
  

Accessing an index of an array with braces instead of brackets has any special significance?

No.

  

Why is it possible to use both forms?

The manual says it can . No explanation why have this alternative. I can not imagine a plausible taste. The bfavaretto spoke of a theory about helping the transition of those who came from Perl, a direct competitor at the time. It's a good theory, though I find it a mistake to make this choice. There is no gain in having this alternative. There is so much more radical about language differences.

  

Using keys would be considered non-standard?

Strictly not.

Since you've never seen anyone use it, do not be the one to start :) It creates confusion, since the keys are usually used for something else, which alias already has more than one purpose, do not add one more. Leave the keys to use with what everyone is accustomed to. Unless I find a good reason, which I doubt.

    
04.01.2017 / 20:45