Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

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

Error I get is Parse error...

Help.

share|improve this question

3 Answers 3

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.

share|improve this answer
    
Cool but why? Can you point me to an article explain this? –  Haim Bender Dec 27 '09 at 13:45
    
No specific reason is mentioned in the PHP array docs although one of the comments mentions something similar. Click on 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 Dec 27 '09 at 18:55
    
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 Dec 27 '09 at 18:59

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.

share|improve this answer
1  
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 Dec 27 '09 at 3:10
    
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 Dec 27 '09 at 3:11
    
+1 for the additional insight. –  David Thomas Dec 27 '09 at 3:12
    
@Jonathan Sampson: "You can't pull immediately from arrays during creation" <-- why? –  Haim Bender Jan 2 '10 at 23:23
    
@Haim, It's just not a feature of PHP yet. I think it's coming though. –  Jonathan Sampson Jan 3 '10 at 0:27

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;
}
share|improve this answer
1  
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 Dec 27 '09 at 3:26
    
@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 Dec 27 '09 at 3:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.