不同版本的PHP语法错误

I sometimes get values from an array like this: $var = array ('key1' => 'value1')['key1']; , so $var should be equal to value1

I run code like this in a server having PHP v5.4.16 , for example, explode ('-', $str)[0]; and it works fine. Now if I transfer this code to another server which uses PHP v5.3.10 I get an error (syntax error): syntax error, unexpected '[' ...

Is this because of the version? (I don't think so because the difference between versions is so small..), or some setting in the server? Can anyone enlighten me?

Yes, it depends on the version of PHP you are running. As PHP docs mentions

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

As of PHP 5.5 it is possible to array dereference an array literal.

In PHP 5.3 you would have to use

$exploded = explode('-', $str);
$first = $exploded[0];
// or
list($first,) = explode('-', $str);

In PHP 5.4 and later you can use

$first = explode('-', $str)[0];