BRG
BRG

Reputation: 380

Get Values of Multidimensional array inside of another array

Hi i am having this structure:

$GLOBALS['config'] = array(
            'mysql' => array(
                    'host' => 'localhost',
                    'username' => 'root',
                    'password' => 'root',
                    'dbname' => 'database'
                ),
            'session' => array(
                    'session_name' => 'user'
                ),
            'remember' => array(
                    'cookie_name' => 'hash',
                    'cookie_expiry' => 604800
                ),
            'folder' => array(
                    'root' => 'backend',
                    'header' => 'head',
                    'views' => 'views'
                ),
            'database' => array(
                    'names' => 'utf8mb4',
                    'charset' => 'utf8mb4',
                    'collation' => 'utf8mb4_general_ci',
                    'driver' => 'pdo'
                ),
            'url' => array(
                'base_url' => 'http://www.example.com/backend/',
                'document_root' => $_SERVER['DOCUMENT_ROOT'] . "/backend"
                ),
            'languages' => array(
                    'english' => 'en',
                    'german' => 'de',
                    'greek' => 'gr'
                ),
            'headers' => array(
                    '404' => 'HTTP/1.0 404 Not Found',
                    '401' => 'HTTP/1.0 401 Unauthorized',
                    '500' => 'HTTP/1.0 500 Internal Server Error',
                    '403' => 'HTTP/1.0 403 Forbidden'
                ),
            'title' => array(
                    'login' => 'Admin Dashboard',
                    'register' => 'Admin Dashboard | User Registration',
                )
        );

and i want in the url/base_url to be like this

'base_url' => 'http://www.example.com/'.$GLOBALS['config']['folder']['root'].'/'

so if i change folder i only have to change the name in only place, but i get a Syntax error like:

Notice: Undefined index: config in C:\xampp-php56\htdocs\backend\core\init.php on line 31

Is possible what i am trying to do? and if possible how?

Upvotes: 0

Views: 61

Answers (2)

jszobody
jszobody

Reputation: 28911

You can't access another array index while you're still defining the array. Your statement defining the array hasn't completed yet, the array isn't available for access until the initial statement is finished.

You need to setup your array as much as you can first, and then go back and add the array elements that reference other array indexes.

So first just create your big array like you're doing, without the base_url.

$GLOBALS['config'] = array(
    ...
);

Now go back and add the url/base_url, you can now access the array index of config.

$GLOBALS['config']['url']['base_url'] = 'http://www.example.com/'.$GLOBALS['config']['folder']['root'].'/';

Example: https://3v4l.org/M7itf

Upvotes: 2

GONG
GONG

Reputation: 4026

Missed dot 'base_url' => 'http://www.example.com/'.$GLOBALS['config']['folder']['root'].'/'

Upvotes: 1

Related Questions