2017-08-09 74 views
0

是否可以從文本文件創建多維數組?如果是,那怎麼可能實現?PHP:文本文件到多維數組

我正在'國家'(奧地利AT,澳大利亞AU,比利時BE)苦苦掙扎,因爲這只是一個新國家開始時才列出的一次。希望這是有道理的。每個國家/地區的條目數可能有所不同。

這將是一個典型的文本文件的例子。

Austria AT 
Vienna-S03-I01 
30 Users 
Vienna-S03-I02 
31 Users 
Australia AU 
Sydney-S01-I01 
49 Users 
Sydney-S01-I02 
46 Users 
Belgium BE 
Brussels-S01-I01 
39 Users 
Brussels-S01-I02 
32 Users 

這就是我想實現:

0 => 
    0 => AT 
    1 => Vienna-S03-I01 
    2 => 30 Users 
    1 => 
    0 => AT 
    1 => Vienna-S03-I02 
    2 => 31 Users 
    2 => 
    0 => AU 
    1 => Sydney-S01-I01 
    2 => 49 Users 
    3 => 
    0 => AU 
    1 => Sydney-S01-I02 
    2 => 46 Users 
    4 => 
    0 => BE 
    1 => Brussels-S01-I01 
    2 => 39 Users 
    5 => 
    0 => BE 
    1 => Brussels-S01-I02 
    2 => 32 Users 

提前感謝!

回答

0

你有什麼試過?

你可以嘗試這樣的事:

$handle    = fopen('file.txt', 'r'); 
$output    = []; 
$currentCountryCode = ''; 
$currentSet   = []; 
if ($handle) { 
    while (($line = fgets($handle, 4096)) !== false) { 
     $line = trim($line); 
     if (preg_match('/^([a-zA-Z,\'\"]*)\s([A-Z]{2})$/', $line, $matches)) { 
      // var_dump($matches);exit; 

      // This is a country line, we need to reset the current set 
      $currentSet   = []; 
      $currentCountryCode = $matches[2]; 
     } else if (preg_match('/^([a-z\-0-9]*)$/i', $line, $matches)) { 
      // If the current set is empty, add the current country code 
      if (count($currentSet) === 0) { 
       $currentSet[] = $currentCountryCode; 
      } 
      // This is a region line 
      $currentSet[] = $line; 
     } else if (preg_match('/^([0-9]{2}\sUsers)$/', $line, $matches)) { 
      // This is a users line 
      $currentSet[] = $line; 
      // We need to add the set to the output array 
      $output[] = $currentSet; 
      // And reset it 
      $currentSet = []; 
     } else { 
      $error = 'Could not this match line! Need to update the regexes!' . PHP_EOL . var_export(['line' => $line], true); 
      throw new Exception($error); 
     } 
    } 
    fclose($handle); 
} 
var_export($output); 

測試中PHP7.0.20

| => php test.php 
array (
    0 => 
    array (
    0 => 'AT', 
    1 => 'Vienna-S03-I01', 
    2 => '30 Users', 
), 
    1 => 
    array (
    0 => 'AT', 
    1 => 'Vienna-S03-I02', 
    2 => '31 Users', 
), 
    2 => 
    array (
    0 => 'AU', 
    1 => 'Sydney-S01-I01', 
    2 => '49 Users', 
), 
    3 => 
    array (
    0 => 'AU', 
    1 => 'Sydney-S01-I02', 
    2 => '46 Users', 
), 
    4 => 
    array (
    0 => 'BE', 
    1 => 'Brussels-S01-I01', 
    2 => '39 Users', 
), 
    5 => 
    array (
    0 => 'BE', 
    1 => 'Brussels-S01-I02', 
    2 => '32 Users', 
), 
) 

您可能需要調整正則表達式,因爲我還沒有實際測試過這個腳本。此外,只有當您的其他數據的結構與您提供的第一行完全一致時,此功能纔有效。祝你好運!

+0

感謝您的幫助。我非常喜歡這個主意。我測試了它,它正在處理的例外是它不添加國家代碼。這就是我得到:'0 => 0 =>維也納-S03-I01 1 => 30個用戶 1 => 0 =>維也納-S03-I02 1 => 31個用戶 2 => 0 =>悉尼-S01-I01 1 => 49個用戶 3 => 0 =>悉尼-S01-I02 1 => 46個用戶 4 => 0 =>布魯塞爾-S01-I01 1 => 39用戶 5 => 0 => Brussels-S01-I02 1 => 32用戶'雖然 – khofm

+0

不會觸發錯誤信息它看起來像'if(count($ currentSet)=== 0){'永遠不會因爲該集合從不爲空而被觸發。 – khofm

+0

不要試圖表達自己的意思,但我想你還應該注意上面的其他答案,因爲可以根據OP的要求命名和進一步處理陣列。 – msalperen

0

也許這可以爲你工作(假設你將通過文件讀取獲得$行)。我曾與http://phpfiddle.org/測試,您還可以在那裏運行這段代碼,看看輸出是你想要

<?php 

$lines = ("Austria AT 
Vienna-S03-I01 
30 Users 
Vienna-S03-I02 
31 Users 
Australia AU 
Sydney-S01-I01 
49 Users 
Sydney-S01-I02 
46 Users 
Belgium BE 
Brussels-S01-I01 
39 Users 
Brussels-S01-I02 
32 Users"); // Get the file content 

    $arr = []; 
    $header = FALSE; 

$lines = preg_split("/\\r\\n|\\r|\\n/", $lines); 

    foreach($lines as $line){ 
     if(preg_match("/(.*)\s[A-Z]{2}/", $line)) 
      $header = $line; 
     if($header != $line) 
      $arr[$header][] = $line; 
    } 

    print_r($arr); 

?> 

,我得到的輸出是什麼是一樣的東西:

Array ([Austria AT] => Array ([0] => Vienna-S03-I01 [1] => 30 Users [2] => Vienna-S03-I02 [3] => 31 Users) [Australia AU] => Array ([0] => Sydney-S01-I01 [1] => 49 Users [2] => Sydney-S01-I02 [3] => 46 Users) [Belgium BE] => Array ([0] => Brussels-S01-I01 [1] => 39 Users [2] => Brussels-S01-I02 [3] => 32 Users)) 

即可進入各個元件,如:

print_r($arr['Austria AT']); 

它返回:

Array ([0] => Vienna-S03-I01 [1] => 30 Users [2] => Vienna-S03-I02 [3] => 31 Users) 

print_r($arr['Austria AT'][1]); 

返回

30 Users