2017-08-09 68 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