2014-03-31 115 views
0

我有邮政编码的文本文件导入到一个关联数组像这样的关键搜索:PHP - 通过关联数组

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 
) 

我需要什么才能够do是从输入框中获取郊区名称(我可以很容易地做到这一点),然后使用该字段搜索键并返回它的值。这对于一个独特的郊区名称非常适用,但在墨尔本这样的郊区有多个邮政编码时会下降。我已经使用了PHP功能array_key_exists,但这只能给一个郊区。

我已经发现这是因为我的数组安装不正确。而不是存储多个值的关键墨尔本,它分配最后一个它看到=> 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 
+0

夫妇明显这里的东西。 '钥匙是唯一的'邮政编码是独特的,所以他们应该是钥匙。或者如果你必须数组(array(key => val)) – ron

回答

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 
+0

非常感谢!我有点想它可能是这样的,但我有问题转换成代码思想。这是一个多维数组,对吗?我一直在努力与阵列,所以它是很好的重新访问他们在PHP中。 –

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

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

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

+0

我想我明白你的意思了:就像在RiggsFolly的答案 - 数组中的数组一样。谢谢你的提示。 –