user911216
user911216

Reputation: 11

php data structures suggestion needed

I've 1000's of url's in the form of:

http://www.downloadformsindia.com/index.php?option=com_download&e=n&task=showpage&file=Forms%252Fmisc%252Ficc%252Fhdfcbank%252FBlocking%20ATM%20and%20Debit%20Card%20Form.pdf&title=HDFC%3ABlocking%2BATM%2B%26%2BDebit%2BCard%2BForm&code=igi

For each url I want to store related breadcrumb. So whenever an URL like this is visited I'll show it's breadcrumb.

So I want a map to store url's with breadcrumbs. URL's will be hash key.

My problem is that I've not worked on PHP but only on C++/Java or Perl. I want a hash containing say 10 thousand such values in a php variable, which I'd serialize it to store it permanently on the disk. For each page load, I'd create the stored hash variable on the fly and search for the url as a key of the hash. It should be pretty fast. I'm not sure if PHP hash uses a Tree Map or Hash Map. Ok if I go for Hash map, how do I evenly distribute the url's as key so that all the url's do get into single bucket?

Any ideas welcome.

Upvotes: 1

Views: 133

Answers (3)

Manish Nagdewani
Manish Nagdewani

Reputation: 597

You can use php with redis if you want to use data structure here for faster output.You can check below extension.

https://github.com/phpredis/phpredis

Hope this would help you out.

Upvotes: 0

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99909

PHP's associative arrays are Hash tables (Hash maps).

They accept any string as key, so you can use the URLs directly for the keys.

You can just create an array and assign to it like this:

$data = array();
$data[$url] = $data_for_this_url;

And export it with serialize() or even var_export() (the later may be faster to import, especially with an opcode cache).

This is a hash table, so the keys are hashed to be evenly distributed in the table. The table grows as needed to avoid too much collisions. You don't have to take care of this.

Upvotes: 1

Michael Mior
Michael Mior

Reputation: 28752

You can try using a simple associative array. PHP natively allows the use of strings for array indices. I'm not sure about the underlying data structure, but you could always benchmark it.

Upvotes: 1

Related Questions