Reputation: 13
I have this lab problem that I've now been stuck on for three days and I'm pretty sure after countless googling and trying pretty much everything I can find, that I've just confused myself to the point of being utterly lost. I'm not sure if this is what you guys do, but essentially I want the answer with a "how to" of how you got there. In my head I know what I want to do but just cannot fathom getting this into code.
To start, I have a text file, postcode.txt, with a list of postcodes and their respective suburbs like so:
3000,MELBOURNE
3001,MELBOURNE
3002,EAST MELBOURNE
3003,WEST MELBOURNE
This needs to be put into an array with the suburb names as the keys, and the postcodes as the values. As you can see, some suburbs have more than one postcode. Am I correct in thinking this will be a multi-dimensional associative array?
I'm not sure if it's best to use file() or file_get_contents() as I'm very new to PHP and have never even used an array before, let alone something this confusing. I can then assume I need to explode the lines of text by ',' and somehow have the suburbs as keys and the postcodes as values.
Once this is in an array I need to search this array by way of user input for a particular suburb name and it needs to return the value or values of that suburb. From the things that I have tried it isn't returning values at all so I can only assume it's something to do with case sensitivity or white spaces etc.
I'm not entirely sure why this is a lab question when I've yet to have any lab questions dealing with simple arrays, but nothing I can do except desperately try and understand this. It's been driving me mad. Any help is very much appreciated.
Upvotes: 0
Views: 944
Reputation: 24435
To start with, opening the file... file()
is the easiest method to read a file into an array as it is performed with one line.
Drawbacks:
file()
can be slow for larger files, as it needs to read the entire file into an array before you can use itAlternatively, use the standard file open structure using something like fgets()
to read it line by line.
Drawbacks:
Let's use file()
as an example:
$lines = file('postcodes.txt');
The first part of your question is how to get the array structure you want, which is the suburb name as the array key and all the post codes as the values. There are plenty of ways to do this - here's a simple example using a 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;
}
Docs: array_map()
, array_key_exists()
, in_array()
, explode()
, foreach
, continue
An output of the resulting array would yield something like this:
Array
(
[MELBOURNE] => Array
(
[0] => 3000
[1] => 3001
)
[EAST MELBOURNE] => Array
(
[0] => 3002
)
[WEST MELBOURNE] => Array
(
[0] => 3003
)
)
Searching is a kettle of fish, and there are many things to consider. Do you want to return a case sensitive result, case insensitive, partial results, multiple results etc?
Here are some options and what you should use for them:
array_keys($postcodes, 'suburb name')
strstr()
(case sensitive) or stristr()
(case insensitive) matching the key and the search term and killing the loop if it is foundHere's an example function to return all partially matches results from the array:
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;
}
Example use:
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
)
)
Looking forward, you might also want to match any of the search terms passed in as a string, e.g. "east west" to match both east and west Melbourne. In this case you'll need to explode the search string to spaces, and perform a search on each term. You'll need to ensure to only return unique values here. Here's an example of a function that would do that:
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;
}
And given you search for "east west", the result would be:
Array
(
[EAST MELBOURNE] => Array
(
[0] => 3002
)
[WEST MELBOURNE] => Array
(
[0] => 3003
)
)
This kind of data structure would be best to be stored in a database if it's going to be used more than once, but to parse a text/CSV file this is how you'd approach it. Hope this helps.
Upvotes: 3