Home » Php » Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?

Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to access nested array element like this:

$dbSettings = $sm->get( 'Config' )[ 'doctrine' ][ 'connection' ][ 'orm_default' ][ 'params' ];

It’s inside Module.php of Zend’s framework 2 project. $sm->get('Config') return an array which I can access with code above locally, with PHP 5.4, while doing so on client’s machine, it gives me an error:

Parse error: syntax error, unexpected '[' in /home/.../azk/module/Main/Module.php on line 121

Is there any difference in PHP 5.3 <=> 5.4 in accessing nested arrays or I have some default PHP configuration which is set differently on clients machne?

How to&Answers:

Array dereferencing, which is what you are using, was introduced in PHP 5.4 and won’t work in PHP 5.3.

So

$dbSettings = $sm->get( 'Config' )[ 'doctrine' ][ 'connection' ][ 'orm_default' ][ 'params' ];

Would need to be:

$dbSettings = $sm->get( 'Config' );
$params     = $dbSettings[ 'doctrine' ][ 'connection' ][ 'orm_default' ][ 'params' ];