PHP:使用给定的结构从config.cfg获取变量

时间:2017-10-08 07:44:32

标签: php config

对于专业人士而言,这可能是一个愚蠢的问题,但即使经过数小时的谷歌搜索,我找不到解决方案:

我有一个具有以下结构的共享config.cfg(因此我无法对结构做任何事情):

[config]
fileExtensions="JPEG|JPG|jpg|jpeg|png|gif"
contentFolder="Gallery"

[images]
maxSize=1900
jpegQuality=85

[thumbnails]
jpegQuality=80
blurredImageQuality=3

我设法将config.cfg放到我的php脚本中,并且可以调用一些变量,例如' maxSize'。现在来了,但是:

我当前的解决方案只是将config.cfg加载为一个简单的数组:

Array ( [fileExtensions] => JPEG|JPG|jpg|jpeg|png|gif [contentFolder] => Gallery [sortOrder] => asc [titleDescSeparator] => $$ [albumCoverDetector] => @@@@@ [ignoreDetector] => _hidden [maxSize] => 1900 [jpegQuality] => 80 [blurredImageQuality] => 3 [allowedSizeValues] => 50|100|225|150|200|300|auto [allowOrigins] => * )

但是为了获得config.cfg中两次的变量,我需要加载包含结构(包含数组和子数组)的config文件。任何人都知道如何做到这一点?继承了我目前的代码:

<?php
$config_values = parse_ini_file('./nano_photos_provider2.cfg');
print_r($config_values); //call whole array
echo $config_values[maxSize]; //call single variable
?>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

In a manual that you should read first there's a second argument which is:

By setting the process_sections parameter to TRUE, you get a multidimensional array, with the section names and settings included.

So, you just need to:

$config_values = parse_ini_file('./nano_photos_provider2.cfg', true);
print_r($config_values); //call whole array
echo $config_values['images']['maxSize']; //call single variable