Solved
PHP most significant string match in array.
Posted on 2009-06-30
Hello,
I am writing a small billing application to bill phone records. I would be having an array which would load and hold the tariff information. The array would be something like:
$Tariff
(
[prefix] = 91, [rate] = 0.51
[prefix] = 92, [rate] = 0.80
[prefix] = 94, [rate] = 0.25
[prefix] = 947, [rate] = 0.33
[prefix] = 65, [rate] = 0.12
[prefix] = 66, [rate] = 0.15
[prefix] = 965, [rate] = 0.52
)
Although I've only shown about 4 entries, the array can have about 25,000 Entries having country code / prefix and rate information.
Now, what I would like to do is, when I pick up a phone number to bill, I'd like to find the most significant match to bill the number. For instance, lets say, my phone number to be billed is:
$called_number = '94777191934';
As its clear, this number can be billed using either prefixes 94 or 947 (based on the tariff array), however, the most significant match is, 947, so the billing rate would be 0.33.
I need to find a way, that would effectively be able to find which prefix to use from the array to bill the number. If I use functions which allow to find the needle in the haystack [i.e, strstr()], then possible matches in this case would be 94, 947 and even 91, but what I'm looking for is, something that would only match at the beginning of the number, and the most significant match.
The requirement here is for find out a way that would be able to achieve this in a SUPER FAST way. I'd be requiring to process thousands of records every minute and matching part has to be executed very fast to avoid any bottlenecks. So what would be the most effective way to do it?
------------------------------------------------------------------------------------------------
Part 2:
As I mentioned earlier, the Tariff array can have about 25,000 entries in it. I've been wondering, perhaps it would be not a wise idea to search the entire array for a match.
Assuming the number to be billed is '94777191934', what I thought of doing is, create a second temporary array [$Tariff_Temp()] and copy all entries from $Tariff() to $Tariff_Temp that starts with '9'. This way, I'd have a small array to do the search and matching. Once the match has been found, then I clear the temp array, check the next number to be billed, and populate the temp array based the number again.
Is it a good idea to work this way? The primary purpose is to keep the dataset small and manageable to achieve efficiency and speed in processing. If its a good idea, then what is a best and fastest way to copy data from the Tariff array to the Temp array based on the above condition?
Any help appreciated.
Cheers
Shaf.