如何读取包含数组的PHP文件

时间:2016-06-10 18:05:49

标签: php arrays

我有一个php文件,config.php,它包含以下代码。

$_config = array(

    'db' => array(
        'mysolidworks' => array(
        'host'      => 'localhost',
        'username'  => 'netvibes',
        'password'  => 'frakcovsea',
        'dbname'    => 'sw_mysolidworks',
        'driver_options' => array(
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
        )
    )
  )
);

我想将此文件读入变量,然后像数组一样读取变量。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

只需按照其工作方式使用PHP:

include('config.php');
echo $_config['db']['mysolidworks']['host'];

答案 1 :(得分:0)

您可以使用以下任何一种

  <?php
        $file_loc = 'SomeDir/config.php';
        require $file_loc; //Throw a fatal if it's not found
        require_once $file_loc; //If this might be called somewhere else in your script, only include it once
        include $file_loc; //Include but just show a warning if not found
        include_once $file_loc; //Include if not already included and throw warning if not
  ?>