PHP - 按关联数组键搜索

时间:2014-03-31 23:46:58

标签: php arrays associative-array

我有一个邮政编码文本文件,可以导入到一个关联数组中,如下所示:

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 存储多个值,而是分配它看到的最后一个值=> 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

3 个答案:

答案 0 :(得分:1)

要通过键“搜索”,只需使用密钥。

$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
    }
}

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

答案 2 :(得分:0)

因此,您需要的是在单个插槽中存储多个值的某种方式。你已经知道怎么做了;毕竟,您正在使用数组在$postcodes“插槽”中存储多行。

阅读how arrays work,特别注意$array[] = <expression>语法。完成后,您需要修改打印出结果的代码。 (你能看到如何以及为什么?)