How to find the front and back of an element in a hash array in PHP

Source: Internet
Author: User
This title looks more like a detour, I directly on the code, such as the following a form of organized array

$a = Array (    ' key12 ' = =    12323,    ' key32 ' =    =    4345,    ' key13 ' =    323423,    ' key43 '    and 32423,    ' key25 ' and '    33423 ';

For an unknown array, I know the key value of any of the existing elements, for example key13 , how do I know key13 which two keys are before and after each other? For example, in this example, how can I know $a and key13 , find out key32 and key43 what?

This hash array does not have sequential numeric key values, so it is not possible to get the precursor and successor for the key value +1 or -1 , do you have any good idea?

Reply content:

This title looks more like a detour, I directly on the code, such as the following a form of organized array

$a = Array (    ' key12 ' = =    12323,    ' key32 ' = =    4345,    ' key13 '    = = 323423,    ' key43 '    and 32423,    ' key25 '    +    33423);

For an unknown array, I know the key value of any of the existing elements, for example key13 , how do I know key13 which two keys are before and after each other? For example, in this example, how can I know $a and key13 , find out key32 and key43 what?

This hash array does not have sequential numeric key values, so it is not possible to get the precursor and successor for the key value +1 or -1 , do you have any good idea?

You can refer to the following Php-search array keys and return the index of matched Key-stack Overflow, the idea is to find out the index of key, and then subtract 1 or plus 1 according to the index to get the item before and after: http://s Tackoverflow.com/questions/37 ...

I wrote a test code:

 
      12323,    ' key32 '    =    4345,    ' key13 '    and    323423,    ' key43 '    and    32423,    ' key25 '    =    33423);p rint_r ($a); echo "
"; $key = ' key13 ';//The index number of the search key $keys = Array_keys ($a); $key _index = Array_search ($key, $keys); echo" Index of $key is $key _ind Ex
";//the key index number minus 1 or plus 1 gets the preceding and latter indexes (note to determine whether or not to cross) if ($key _index = = 0) echo ' no pre key
' Else echo ' pre key is '. $keys [$key _index-1]. '
' If ($key _index < count ($keys)) echo ' next key is '. $keys [$key _index + 1]. '
' Else echo ' no next key
';? >

Null is the right (already point of praise), at the PHP function level (Zend API level I do not know), there is no way to bypass the array traversal, the code is probably like this (the pseudo-code, can not be executed, the following):

 

If you are looking for a value that is not the inverse of the array first/two, the entire array is not traversed.

But this kind of writing to your code ability requirements higher, the code is a bit larger, look not intuitive, you should pay attention to:
1. The current () element value is error-prone when it is exactly false, you must use the = = = comparison operator
2. The while loop first and last round to be special, because the value you are looking for may be exactly at the end of the array, without prev or next
3. Current,next,prev the value of the element, but also moved the array of internal pointers, these functions are relatively small, the maintenance of the person may not read

Using He Zhiqiang's program, the code is much more concise:

 
   $mid _number_index) {    $next _key = $mid _number_index+1;    $next _value = $a [$next _key];}

Obviously, there are two areas where the performance is slightly worse:
1.array_keys () Be sure to traverse all array elements
2.array_search () The search is also traversed (but found in the middle of the break)

If your array is not very large, it is recommended to write code with He Zhiqiang's program.

The array of PHP is implemented by the structure of the hash+ doubly linked list. Reference PHP Source: zend/zend_hash.h

typedef struct _HASHTABLE {UINT ntablesize;uint ntablemask;uint nnumofelements;ulong nnextfreeelement; Bucket *pinternalpointer;/* used for element traversal */bucket *plisthead; Bucket *plisttail; Bucket **arbuckets;dtor_func_t pdestructor;zend_bool persistent;unsigned Char napplycount;zend_bool bApplyProtection ; #if zend_debugint inconsistent; #endif} HashTable;

Where Pinternalpointer is the inner pointer to the array. If the internal pointer points to the position OK, you can get the front and back item pointers for the linked list. However, there is currently no external function that PHP provides internal pointer that sets the array based on key. So the linear traversal is unavoidable.

See a relatively concise approach:

$a = Array (    ' key12 ' = =    12323,    ' key32 ' =    =    4345,    ' key13 ' =    323423,    ' key43 '    =    32423,    ' key25 ' and ' 33423 ',    while (key ($a)!== ' key13 ') next ($a); $prev _val = prev ($array); # The previous item's Value$prev_key = key ($array); # previous Key

Get the latter item using the next function, similar to the method.

Reference: Http://stackoverflow.com/questions/47 ...

Iterate through the array and then prev/next these functions, or extract keys to form a new array, and then use value (the original key value) to take a digital subscript, and then the key

Generally, except for these, it doesn't seem like a good idea.

The methods mentioned above all need to pass through the entire array, the efficiency is lower, limited to the language level of the reasons, only the use of the PHP language itself can only achieve this level.

But as @liruqi students ' answer, PHP is actually implemented using the hash+ doubly linked list, so if you write a C extension to PHP, you can use an O (1) time to get the answer you want.

  • 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.