繁体   English   中英

在PHP中将文本文件转换为多维关联数组

[英]Converting text file into multi-dimensional associative array in PHP

我遇到了这个实验室问题,现在已经坚持了三天,并且可以肯定的是,经过无数次谷歌搜索和尝试了几乎所有可以找到的一切之后,我确信自己已经迷失了自己,完全迷失了自己。 我不确定这是否是你们的工作,但从本质上讲,我希望答案是“如何”到达那里的。 在我的脑海里,我知道我想做什么,但无法理解将其纳入代码中。

首先,我有一个文本文件postcode.txt,其中包含邮政编码列表及其各自的郊区,如下所示:

3000,MELBOURNE
3001,MELBOURNE
3002,EAST MELBOURNE
3003,WEST MELBOURNE

这需要放入一个以郊区名称为键,以邮政编码为值的数组。 如您所见,有些郊区有多个邮政编码。 我认为这将是一个多维关联数组,对吗?

我不确定是否最好使用file()或file_get_contents(),因为我对PHP还是很陌生,甚至从未使用过数组,更不用说这令人困惑了。 然后,我可以假设我需要用','分隔文本行,并以某种方式将郊区作为键,将邮政编码作为值。

将其放入数组后,我需要通过用户输入在该数组中搜索特定的郊区名称,并且它需要返回该郊区的一个或多个值。 从我尝试过的东西来看,它根本没有返回任何值,因此我只能认为这与区分大小写或空格等有关。

当我还没有处理简单数组的任何实验室问题时,我不能完全确定为什么这是一个实验室问题,但是我无能为力,除非拼命尝试理解这一点。 这让我发疯了。 很感谢任何形式的帮助。

读取文件

首先,打开文件... file()是将文件读入数组的最简单方法,因为它只用一行执行。

缺点:

  • 对于较大的文件, file()可能会变慢,因为在使用它之前,它需要将整个文件读入数组

或者,使用标准文件打开结构fgets()fgets()逐行读取它。

缺点:

  • 需要更多代码行

让我们以file()为例:

$lines = file('postcodes.txt');

获取数组结构

问题的第一部分是如何获取所需的数组结构,以郊区名称作为数组键,将所有邮政编码作为值。 有很多方法可以做到这一点-这是一个使用foreach的简单示例:

// Define an empty array to start with
$postcodes = array();
// Loop each line
foreach($lines as $line) {
    // Split the line by the comma and define the variables with each part
    // mapping trim to the array to remove any whitespace on either end
    list($postcode, $suburb) = array_map('trim', explode(',', $line));
    // Check the array key exists in post codes
    if(!array_key_exists($suburb, $postcodes)) {
        // If not, define it to start with
        $postcodes[$suburb] = array();
    }

    // Check if the postcode is already in the array
    if(in_array($postcode, $postcodes[$suburb])) {
        // Skip this postcode, it's already where it should be
        continue;
    }

    // Add the postcode to it
    $postcodes[$suburb][] = $postcode;
}

文件: array_map()array_key_exists()in_array()explode()foreachcontinue

结果数组的输出将产生如下内容:

Array
(
    [MELBOURNE] => Array
        (
            [0] => 3000
            [1] => 3001
        )

    [EAST MELBOURNE] => Array
        (
            [0] => 3002
        )

    [WEST MELBOURNE] => Array
        (
            [0] => 3003
        )

)

搜索数组

搜索是一锅鱼,有很多事情要考虑。 您是否要返回区分大小写的结果,不区分大小写的结果,部分结果,多个结果等?

以下是一些选项以及应使用的选项:

  • 完全匹配(单个): array_keys($postcodes, 'suburb name')
  • 完全匹配(多个):使用数组键时不会发生(按定义唯一)
  • 部分匹配(单个):循环,匹配stristr()和搜索词的strstr() (区分大小写)或stristr() (不区分大小写),如果找到则终止循环
  • 部分匹配(多个):与上面相同,但不要杀死找到的循环,而要添加到匹配结果数组中并返回

这是一个示例函数,用于返回数组中所有部分匹配的结果:

function search($postcodes, $search_term) {
    // Define empty array for matches
    $matches = array();
    // Trim the search string to remove whitespace from either end
    $search_term = trim($search_term);
    // Loop through postcodes
    foreach($postcodes as $suburb => $post_codes) {
        // Case insensitive comparison
        if(stristr($suburb, $search_term)) {
            // It matches! Add entire result to return array
            $matches[$suburb] = $post_codes;
        }
    }
    // Return result
    return $matches; 
}

使用示例:

print_r($search($postcodes, 'melbourne'));
print_r($search($postcodes, 'east'));

Array
(
    [MELBOURNE] => Array
        (
            [0] => 3000
            [1] => 3001
        )

    [EAST MELBOURNE] => Array
        (
            [0] => 3002
        )

    [WEST MELBOURNE] => Array
        (
            [0] => 3003
        )

)
Array
(
    [EAST MELBOURNE] => Array
        (
            [0] => 3002
        )

)

展望未来,您可能还想匹配以字符串形式传递的任何搜索词,例如“ east west”以匹配墨尔本的东西方。 在这种情况下,您需要将搜索字符串分解为空格,然后对每个术语进行搜索。 您需要确保此处仅返回唯一值。 这是一个可以做到这一点的函数的例子:

function search_multi($postcodes, $search_term) {
    // Define empty array for matches
    $matches = array();
    // Trim the search string
    $search_term = trim($search_term);
    // Get all search terms
    $search_terms = explode(' ', $search_term);
    // Loop through search terms
    foreach($search_terms as $term) {
        // Loop through postcodes
        foreach($postcodes as $suburb => $post_codes) {
            // First, check that this result hasn't already been found! (unique)
            if(array_key_exists($suburb, $matches)) {
                // It's already been found, skip this one...
                continue;
            }
            // Case insensitive comparison
            if(stristr($suburb, $term)) {
                // It matches! Add entire result to return array
                $matches[$suburb] = $post_codes;
            }
        }    
    }
    // Return result
    return $matches; 
}

并给您搜索“东西”,结果将是:

Array
(
    [EAST MELBOURNE] => Array
        (
            [0] => 3002
        )

    [WEST MELBOURNE] => Array
        (
            [0] => 3003
        )

)

结论

如果要多次使用这种数据结构,最好将其存储在数据库中,但是要解析文本/ CSV文件,这就是您要采用的方法。 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM