tags:

views:

83

answers:

3

Why does the following code give me an error in php?:

$b = array("1" => "2")["1"];

Error I get is Parse error...

Help.

+4  A: 

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.

Jonathan Sampson
Actually, your 2nd reason ("keys of numerical values are automatically converted to integers") doesn't really break his code. If you break up the array assignment and array access, the correct value is accessed. PHP does some type magic and finds the right index.
Asaph
Correct, Asaph. I didn't mean to suggest it was the cause of his problem, only something to keep in mind since it was included in his example.
Jonathan Sampson
+1 for the additional insight.
David Thomas
@Jonathan Sampson: "You can't pull immediately from arrays during creation" <-- why?
Haim Bender
@Haim, It's just not a feature of PHP yet. I think it's coming though.
Jonathan Sampson
+5  A: 

Unfortunately, in PHP, you need to do this:

$a = array("1" => "2");
$b = $a["1"];

It feels like your example should work because it does in other languages. But this is just the way PHP is.

Asaph
Cool but why? Can you point me to an article explain this?
Haim Bender
No specific reason is mentioned in the PHP array docs although one of the comments mentions something similar. Click on http://php.net/manual/en/language.types.array.php and search the page for ")[" and you'll find the comment by "John at nowhere dot com" which does nothing to shed any light on the question. Perhaps a future enhancement to the PHP language will offer support for this type of syntax. In the meantime, you'll have to simply stick with the 2 line syntax.
Asaph
As a sidenote, It makes little sense to write `$b = array("1" => "2")["1"]` because the array is discarded immediately. The same thing can be written as simply `$b = "2"`. The use case of returning an array from a function and immediately accessing an element of the array (as mentioned by "John at nowhere do com" in the comment I mentioned above) is much more practical.
Asaph
A: 

You can use a function to do this for you:

function Get($array, $key, $default = false)
{
    if (is_array($array) === true)
    {
     settype($key, 'array');

     foreach ($key as $value)
     {
      if (array_key_exists($value, $array) === false)
      {
       return $default;
      }

      $array = $array[$value];
     }

     return $array;
    }

    return $default;
}

And use it like this:

$b = Get(array("1" => "2"), "1"); // 2

If you don't need to access multi-dimensional arrays you can also use this shorter function:

function Get($array, $key, $default = false)
{
    if (is_array($array) === true)
    {
     return (array_key_exists($value, $array) === true) ? $array[$value] : $default;
    }

    return $default;
}
Alix Axel
I suppose you could use a function but why such a long one? Why not something like `function getArrayValue(array $a, $index, $default=null) { return array_key_exists($index, $a) ? $a[$index] : $default;}`
Asaph
@Asaph: I don't see the harm in having "long" functions as long as they are useful, my first function allows to fetch multi-dimensional indexes and the performance is basically the same.
Alix Axel