PHP:Textfile到多维数组

时间:2017-08-09 19:42:43

标签: php arrays multidimensional-array

是否可以从文本文件创建多维数组?如果是,那怎么可能实现呢?

我正在与'Country'(奥地利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

提前致谢!

2 个答案:

答案 0 :(得分: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',
  ),
)

您可能需要调整正则表达式,因为我还没有真正测试过这个脚本。此外,只有当您的其他数据的结构与您提供的第一行完全相同时,这才有效。祝你好运!

答案 1 :(得分: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