PHP associative arrays are not specified with hash table (hash tables)

Source: Internet
Author: User

One of the most important data types in PHP is an associative array, also known as a hash table, which is a very useful data structure.

In the program, we may encounter problems that need to be eliminated, and give the simplest model:

There is a list of user names, stored 10,000 user names, no duplicates;

There is also a list of blacklist, stored 2000 user names, the format and user name list is the same;

Now you need to remove the username from the list of user names in the blacklist and ask to be processed as quickly as possible.

This problem is a small amount of processing, if the actual point, 2 tables can be very large, such as 200 million records.

I first thought of the method is to do a nested loop, set the user name table has M record, blacklist list has N records, then, the number of cycles is M * N times!

PHP Version Code:

<?php  
foreach ($arrayM as $keyM => $nameM) {  
foreach ($arrayN as $nameN) {  
if ($nameM = = $nameN) {  
// The bank executed the M * N times!  
unset ($arrayM [$keyM]);  
}} return $arrayM;  
? >

Another way to take advantage of an array index.

PHP is a weak type of language and does not have a strict variable-type limit like the C language. The C language array, each element must be of the same type, and the index starts at 0.

An array of PHP, which can be indexed as a string, also known as an associative array.

Array index, there is a natural limit is not repeat, and access time does not need to find, you can directly locate.

Or the question that we have just now, we have adopted another approach.

The user name of the blacklist list is organized into an array, and the index of the array is the username.

Then, to traverse the list of users, just use Isset to query whether the user name exists.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

PHP Version Code:

<?php  
$arrayHash = Array ();  
foreach ($arrayN as $nameN) {  
//bank executed N times.  
$arrayHash [$nameN] = 1;  
}  
     
foreach ($arrayM as $keyM => $nameM) {  
if ($arrayHash [$nameM])) {  
//bank executed M times!  
unset ($arrayM [$keyM]);  
}  
return $arrayM;  
? >

You can see that the optimized code, the number of cycles is M + N times.

If M and N are 10000, the optimization before the cycle of 100 million times, after optimization, only 20,000 times, the difference is 5,000 times times!

If the second program takes 1 seconds, the first program will take nearly 1.5 hours!

=========================================================================

Hash a seemingly more complex thing, in fact, not so exaggerated understanding, here to make a note.

Hash, Chinese translation into a messy thing, someone also called it hash, or translated into what is not a transliteration "hash."

In short, hash is to get a complex string, through a certain conversion, to obtain a simple number (usually a number).

If "ABCD" uses the value of each character to add directly, then take the remainder of 10, both (a+b+c+d), to get a number, for example, the result is 5, then this 5 can represent the string abcd in some sense. Or this 5 can also be said to be a marker of this string, and is a simplified tag, so someone called this 5 is the summary of the string, or fingerprint.

One of the good uses of this 5 is that it can be used as a subscript for an array, such as I construct a pointer array void* hash_array[10], so I can fill the 5 position with a pointer, such as the ABCD string.

In this case, if I'm going to query whether a string exists, there is no need for an array to use string loops to contrast such a slow operation, and directly get a string of hash value, and then use this hash value, in the array subscript directly to find, so fast a lot of speed, especially when the data is more.

When you can see the hash value calculated above, the result may not start from 0, as we calculate 5. In other words, this 5 is an indeterminate position in the array, or it can be called a hash out position. Other locations may have been empty all the time. That's why this array or table is called a hash table.

But there is a problem, the above conversion method, directly add, and then take a remainder, when the string into ABDC, the result is still the number 5. This is a problem with the above algorithm, that is, it does not guarantee a uniqueness. So there is a lot of hash algorithm research, such as md4,md5,sha-1, to ensure uniqueness.

But the above algorithm can still be used, the practice is in ABDC after the hash get 5, to check whether 5 is occupied, if occupied, then the number plus 1, that is 6, if 6 is not occupied, fill in the value. If one of the following strings calculates a value of 6, but 6 is already occupied, add 1, then save.

Take the data, you can first calculate the hash value, and then look at the content is not what you want, if not, add 1 to see, and finally get one.

So the contents of the hash table are not like the general array of the first organized, but the subsequent slowly into the increase.

The contents of the hash table can generally be a pointer, this pointer can point to a large structure is also possible. This structure can have key, value information.

Hash table can also not be an array, you can organize it into a linked list, the node structure in the chain can have a parameter is the hash_value of that number, used for quick lookup.

Although in many cases the hash is used in the context of encryption, but in general application code, you can also use it to store simple data, so that the efficiency of the code is much higher.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.