Why does the following code give me an error in php?:
$b = array("1" => "2")["1"];
Error I get is Parse error...
Help.
|
Unfortunately, in PHP, you need to do this:
It feels like your example should work because it does in other languages. But this is just the way PHP is. |
|||||||||||||
|
Couple things. You can't pull immediately from arrays during creation, and keys of numerical values are automatically converted to integers, even if they're intended to be strings. |
|||||||||||||||||||||
|
You can use a function to do this for you:
And use it like this:
If you don't need to access multi-dimensional arrays you can also use this shorter function:
|
|||||||||
|