按键名称选择数组值


Select array value by key name

在我收到一片陷阱之前,请相信我-我已经在谷歌上搜索过并使用了SO搜索。

如何按名称选择数组值?

如果我想调用特定的值(对于数据库连接),我该如何在这里做到这一点?以下是基于php的CMS站点的config文件夹中的database.php文件。

我想为mysqli查询创建连接

$db_connection = @mysqli_connect(host,user,pass,database)

下面的代码存在于站点的配置文件夹config/database.php中的一个单独的文件中。

$config['default'] = array(
        'benchmark' => TRUE,
        'persistent' => FALSE,
        'connection' => array(
            'type' => 'mysqli',
            'user' => 'myname',
            'pass' => 'somepass123',
            'host' => 'localhost',
            'port' => FALSE,
            'socket' => FALSE,
            'database' => 'local_sitename',
        ),
        'character_set' => 'utf8',
        'table_prefix' => '',
        'object' => TRUE,
        'cache' => FALSE,
        'escape' => TRUE
    );

互联网和我的课本上都有这样的例子:http://www.homeandlearn.co.uk/php/php6p3.html

例如,其中是季节数组:

<?php
$seasons = array("Autumn", "Winter", "Spring", "Summer");
print $seasons[0];
?>

我知道我可以使用$seasons[2]; 来选择Spring

但是在我的站点配置文件中使用数组会使用数组中的数组。我需要像这样的东西(这在语法上是错误的,但我希望能传达我需要的)

$db_connection = $config(connection(host)),$config(connection(user)),$config(connection(pass)),$config(connection(database))

我该如何称呼这些价值观?

$config['default'] = array(
        'benchmark' => TRUE,
        'persistent' => FALSE,
        'connection' => array(
            'type' => 'mysqli',
            'user' => 'myname',
            'pass' => 'somepass123',
            'host' => 'localhost',
            'port' => FALSE,
            'socket' => FALSE,
            'database' => 'local_sitename',
        ),
        'character_set' => 'utf8',
        'table_prefix' => '',
        'object' => TRUE,
        'cache' => FALSE,
        'escape' => TRUE
    );

echo $config['default']['connection']['user'] // prints myname
echo $config['default']['connection']['pass'] // prints pass
echo $config['default']['connection']['host'] // prints host

使用字符串按键对数组进行索引:

$config['default']['connection']['host'];// === 'localhost'