Home >Backend Development >PHP Tutorial >How to implement array with php hash algorithm_PHP tutorial
Array is the most used one in PHP. So how is Array implemented? In PHP, Array is implemented through a hashtable, in which the chaining method is used to solve hash conflicts. In this way, the complexity of finding Array elements is O(N) in the worst case, and 1 in the best case.
Used in PHP The most common one is Array. So how is Array implemented? In PHP, Array is implemented through a hashtable, in which the chaining method is used to solve hash conflicts. In this way, the complexity of finding Array elements is O(N) in the worst case, and 1 in the best case.
static inline ulong zend_inline_hash_func(const char *arKey, uint nKeyLength)
{
register ulong hash = 5381; //Is there any mystery in the setting of the initial value here?
/* variant with the hash unrolled eight times */
for (; nKeyLength >= 8; nKeyLength -= 8) { //Why is this step=8 method?
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++; // Faster than direct *33
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash ) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
}
switch (nKeyLength) {
case 7: hash = ((hash < ;< 5) + hash) + *arKey++; /* fallthrough... */ //Here is the hash of the remaining characters
case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 1: hash = (( hash << 5) + hash) + *arKey++; break;
case 0: break;
EMPTY_SWITCH_DEFAULT_CASE()
}
return hash;//return hash value
}