Question

I have a text file of postcodes to import into an associative array like this:

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

The lecturer has said that we need to load this into an associative array using the suburb name as the key and the postcode as the value. I've done a bit of research on how to do this and the best I could come up with was :

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

This gives something like this:

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

What I need to be able to do is GET a suburb name from an input box (I can do this easily enough), then use that field to search the keys and return it's value. This works great for a unique suburb name, but falls down when there are multiple postcodes for the one suburb like Melbourne. I've used the PHP function array_key_exists, but this only gives the one suburb.

I've since found out that this is because my array isn't setup correctly. Instead of storing multiple values for key MELBOURNE, it is assigning the last one it sees => 8001

Can someone please help me? I've been spending too long on this and it is killing me. I need it to display something like:

The postcode for MELBOURNE is 3000
The postcode for MELBOURNE is 3001
The postcode for MELBOURNE is 3004
The postcode for MELBOURNE is 8001
Was it helpful?

Solution

As you have a situation where one suburb can have more than one postcode you need to store the data as an array within an array, using the suburb as the key to the outer array.

Like so:-

<?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';
}

The output from this would be

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

OTHER TIPS

To "search" by a key, you need only use the key.

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

Of course, if that key doesn't exist in the array, you'll get an error, so you would first want to check:

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

If you want to return partial matches, something like this:

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

So, what you need is some way to store multiple values in a single slot. You already know how to do this; after all, you're using an array to store multiple lines in the $postcodes "slot".

Read up on how arrays work, paying special attention to the $array[] = <expression> syntax. Once you've done that, you will need to modify the code that prints out your result. (Can you see how and why?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top