【问题标题】:PHP - Searching by Key of associative arrayPHP - 按关联数组的键搜索
【发布时间】:2014-03-31 23:46:57
【问题描述】:

我有一个邮政编码文本文件要导入到这样的关联数组中:

3000,MELBOURNE
3001,MELBOURNE
3002,EAST MELBOURNE
3003,WEST MELBOURNE
3004,MELBOURNE
...
8001,MELBOURNE

讲师说我们需要将其加载到关联数组中,使用 郊区名称作为键邮政编码作为值。我已经对如何做到这一点进行了一些研究,我能想到的最好的方法是:

<?php    
    if(isset($_GET['suburbField'])) {
    $userInput = $_GET['suburbField'];  

    //load the postcodes file into an array
    $postcodes = file('postcode.txt'); 

    //use an associative array - slides 51, 52
    foreach ($postcodes as $lineNum => $line) {
    //str split by comma and into new array - refer slide 17, 18
        list($value, $key) = explode(",", $line);
        $split_postcodes[rtrim($key)] = rtrim($value);

        }
    print_r($split_postcodes); //test that keys and vars assigned correctly
    }

这给出了这样的结果:

Array (
 [MELBOURNE] => 8001,
 [EAST MELBOURNE] => 8002,
 [WEST MELBOURNE] => 3003
)

我需要做的是从输入框中获取郊区名称(我可以很容易地做到这一点),然后使用该字段搜索键并返回它的值。这对于一个独特的郊区名称非常有用,但当像墨尔本这样的一个郊区有多个邮政编码时,它就会失败。我使用了 PHP 函数array_key_exists,但这仅给出了一个郊区。

我后来发现这是因为我的阵列设置不正确。它没有为键 MELBOURNE 存储多个值,而是分配它看到的最后一个值 =&gt; 8001

有人可以帮帮我吗?我在这上面花了太长时间,这让我很生气。我需要它显示如下内容:

The postcode for MELBOURNE is 3000
The postcode for MELBOURNE is 3001
The postcode for MELBOURNE is 3004
The postcode for MELBOURNE is 8001

【问题讨论】:

  • 这里有几个明显的东西。 “钥匙是唯一的”邮政编码是唯一的,所以它们应该是钥匙。或者如果你必须 array(array(key=>val))

标签: php arrays associative-array


【解决方案1】:

当您遇到一个郊区可以有多个邮政编码的情况时,您需要将数据存储为数组中的一个数组,使用郊区作为外部数组的键。

像这样:-

<?php

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

foreach ($postcodes as $line) {
    list($po, $burb) = explode(",", $line);
    $burb = str_replace(PHP_EOL, '', $burb);
    $pcodes[$burb][] = $po;
}
print_r($pcodes);

$userInput = 'MELBOURNE';

if ( array_key_exists($userInput, $pcodes) ) {
    foreach ( $pcodes[$userInput] as $oneOfTheCodes ) {
        echo 'The postcode for ' . $userInput . ' is ' . $oneOfTheCodes . PHP_EOL;
    }
} else {
    echo 'Suburb does not exist';
}

由此产生的输出是

Array
(
    [MELBOURNE] => Array
        (
            [0] => 3000
            [1] => 3001
            [2] => 3004
            [3] => 8001
        )

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

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

)
The postcode for MELBOURNE is 3000
The postcode for MELBOURNE is 3001
The postcode for MELBOURNE is 3004
The postcode for MELBOURNE is 8001

【讨论】:

  • 非常感谢!我有点想它可能是这样的,但我在将这种想法转换为代码时遇到了问题。那是一个多维数组,对吧?我一直在努力处理数组,所以最好在 php 中重新访问它们。
【解决方案2】:

要通过键“搜索”,您只需要使用键即可。

$postcode = $suburbs['EAST MELBOURNE'];

当然,如果数组中不存在该键,你会得到一个错误,所以你首先要检查:

if(isset($suburbs['EAST MELBOURNE'])) {
    // blah
}

如果你想返回部分匹配,像这样:

$search = $_GET['suburb'];  // get the desired search query from GET
foreach($suburbs as $suburb => $postcode) // loop through each suburb, naming the key 'suburb' and the value 'postcode'
{
    if(stristr($suburb, $search)) // if the search string exists within the current suburb
    {
        echo "The postcode for $search is $postcode<br>"; // echo out the line
    }
}

【讨论】:

    【解决方案3】:

    因此,您需要某种方法来在单个插槽中存储多个值。您已经知道如何做到这一点;毕竟,您正在使用数组在$postcodes“插槽”中存储多行。

    阅读how arrays work,特别注意$array[] = &lt;expression&gt; 语法。完成此操作后,您将需要修改打印结果的代码。 (你能明白如何以及为什么吗?)

    【讨论】:

    • 我想我明白了你的意思:正如 RiggsFolly 的回答 - 数组中的数组。感谢您的提示。
    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多