2012-07-25 42 views
2

我已經嘗試過很多次將此配置文件轉換爲多維數組,這意味着我必須讀取config.txt文件,然後我必須將其轉換爲多維數組。我需要幫助或一些建議。將配置(文本文件)轉換爲多維數組

的config.txt:

id=www 
session.timeout=120 

session.server.0.host=127.0.0.1 

session.server.0.port=1111 

session.server.0.id=session1 

session.server.1.host=127.0.0.1 

session.server.1.port=1111 

session.server.1.id=session2 

image.width=640 

image.height=480 

image.watermark.small=wsmall.png 

image.watermark.normal=wnormal.png 

回答

2

像這樣的東西應該工作:

$config = array(); 
foreach(file('config.txt') as $line) { 
    list($keys, $value) = explode('=', $line); 

    $temp =& $config; 
    foreach(explode('.', $keys) as $key) 
    {   
     $temp =& $temp[$key]; 
    } 
    $temp = trim($value); 
} 

閱讀每一行,在一段時間後,你會得到所有的鍵進入$keys和價值爲$valueexplode() -ing on =。然後,使用$temp作爲指向$config陣列的「指針」,我遍歷所有$keys,它們分別由.上的explode() -ing提取,以形成多維陣列。一旦所有的密鑰用完,我將該值分配給該條目,並移至下一行。

你可以看到它在the demo中很好地工作。您的輸入,這將產生一個這樣的數組:

結果的 load_config_file('config.txt')
array(3) { 
    ["id"]=> 
    string(3) "www" 
    ["session"]=> 
    array(2) { 
    ["timeout"]=> 
    string(3) "120" 
    ["server"]=> 
    array(2) { 
     [0]=> 
     array(3) { 
     ["host"]=> 
     string(9) "127.0.0.1" 
     ["port"]=> 
     string(4) "1111" 
     ["id"]=> 
     string(8) "session1" 
     } 
     [1]=> 
     array(3) { 
     ["host"]=> 
     string(9) "127.0.0.1" 
     ["port"]=> 
     string(4) "1111" 
     ["id"]=> 
     string(8) "session2" 
     } 
    } 
    } 
    ["image"]=> 
    array(3) { 
    ["width"]=> 
    string(3) "640" 
    ["height"]=> 
    string(3) "480" 
    ["watermark"]=> 
    array(2) { 
     ["small"]=> 
     string(10) "wsmall.png" 
     ["normal"]=> 
     &string(11) "wnormal.png" 
    } 
    } 
} 
+0

[這裏](http://viper-7.com/p1qnv4)是使用'print_r()'的更好的演示。 – nickb 2012-07-25 18:40:26

1
function load_config_file($filename) 
{ 
    $config = array(); 

    foreach(file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) 
    { 
     if(!preg_match('/^(.+?)=(.*)$/', $line, $matches)) 
     { 
      continue; 
     } 
     $indices = explode('.', $matches[1]); 
     $current = &$config; 
     foreach($indices as $index) 
     { 
      $current = &$current[$index]; 
     } 
     $current = $matches[2]; 
    } 

    return $config; 
} 

 
Array 
(
    [id] => www 
    [session] => Array 
     (
      [timeout] => 120 
      [server] => Array 
       (
        [0] => Array 
         (
          [host] => 127.0.0.1 
          [port] => 1111 
          [id] => session1 
         ) 

        [1] => Array 
         (
          [host] => 127.0.0.1 
          [port] => 1111 
          [id] => session2 
         ) 

       ) 

     ) 

    [image] => Array 
     (
      [width] => 640 
      [height] => 480 
      [watermark] => Array 
       (
        [small] => wsmall.png 
        [normal] => wnormal.png 
       ) 

     ) 

) 
1

Zend Framework有一個稱爲Zend_Config_Ini類,將解析該結構的一個文件到一個目的或數組。

爲了使用它,你需要從Zend框架庫需要3個文件:

  • 的Zend/config.php文件
  • 的Zend /配置/ Ini.php
  • 的Zend /配置/Exception.php

一旦你擁有了這些3個文件,這裏是一個演示解析和訪問值從該文件中的代碼:

require_once 'Zend/Config/Ini.php'; 

$ini = new Zend_Config_Ini('settings.ini'); 
$ini = $ini->toArray(); 

echo $ini['session']['server'][0]['host']; // 127.0.0.1 
echo $ini['session']['server'][1]['id']; // session2 
echo $ini['image']['width'];    // 640 

要從Zend Framework中獲取3個文件,請將它們放在一個名爲Zend的文件夾中,並將其添加到您的PHP文件中,並將Zend所在的目錄添加到您的include_path中。