This title looks like a detour. Let me go to the Code directly, for example, there is an array organized in the following form {code ...} for an unknown array, I know the key values of any existing element, for example, key13. How do I know which two are before and after key13... this title looks like a detour. Let's go directly to the Code, for example, there is an array organized in the following form:
$a = array( 'key12' => 12323, 'key32' => 4345, 'key13' => 323423, 'key43' => 32423, 'key25' => 33423);
For an unknown array, I know the key value of any existing element, for examplekey13
How do I know?key13
Which two keys are used before and after? For example, how can I know$a
Andkey13
, Find outkey32
Andkey43
What about it?
This type of hash array does not have an ordered value, so it cannot+1
Or-1
To obtain the pioneers and successors. I wonder if you have any good solutions?
Reply content:
This title looks like a detour. Let's go directly to the Code, for example, there is an array organized in the following form:
$a = array( 'key12' => 12323, 'key32' => 4345, 'key13' => 323423, 'key43' => 32423, 'key25' => 33423);
For an unknown array, I know the key value of any existing element, for examplekey13
How do I know?key13
Which two keys are used before and after? For example, how can I know$a
Andkey13
, Find outkey32
Andkey43
What about it?
This type of hash array does not have an ordered value, so it cannot+1
Or-1
To obtain the pioneers and successors. I wonder if you have any good solutions?
You can refer to php-Search array keys and return the index of matched key-Stack Overflow. the idea is to find the index where the key is located, and then subtract 1 from the index or add 1 to obtain the items before and after the index: http://stackoverflow.com/questions/37...
I wrote a test code section:
12323, 'key32' => 4345, 'key13' => 323423, 'key43' => 32423, 'key25' => 33423); print_r ($ a); echo"
"; $ Key = 'key13'; // search for the key index number $ keys = array_keys ($ a); $ key_index = array_search ($ key, $ keys ); echo "index of $ key is $ key_index
"; // Subtract the key index number by 1 or add 1 to obtain the index of the previous and subsequent items (Be sure to determine whether the index is out of bounds) 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 correct (I already liked it). At the php function level (I don't knoW Zend API level), there is no way to bypass array traversal, the code is like this (the pseudocode that is handwritten cannot be executed, the same below ):
If the value you are looking for is not the first or second to the last of the array, the entire array will not be traversed.
However, this writing method requires a high level of code capability and a large amount of code, which does not seem intuitive. You should note that:
1. When the element value obtained by current () is exactly false, it is prone to errors and must use the = comparison operator.
2. Special processing is required for the first and last rounds of the while LOOP, because the value you are looking for may happen to the beginning and end of the array, without prev or next
3. When the current, next, and prev retrieve element values, they also move the internal pointer of the array. These functions are relatively small, and the maintenance personnel may not be able to understand them.
Using the solutions provided by HE Zhiqiang, the code is much simpler:
$ Mid_number_index) {$ next_key = $ mid_number_index + 1; $ next_value = $ a [$ next_key];}
Obviously, this solution has two poor performance points:
1. array_keys () must traverse all array elements
2. array_search () must be traversed during search (but break is found in the middle)
If your array is not large, we recommend that you use the solution of He Zhiqiang to write code.
Php array is implemented by the structure of hash + two-way linked list. See php source code: 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;
PInternalPointer is the internal pointer of the array. if the internal Pointer Points to the position OK, you can obtain the item pointer before and after the linked list. however, no external function is provided for setting the array internal pointer based on the key in php. therefore, linear traversal is inevitable.
There is a relatively simple method:
$ A = array ('key12' => 12323, 'key32' => 4345, 'key13' => 323423, 'key43 '=> 32423, 'key25' => 33423); while (key ($ )! = 'Key13') next ($ a); $ prev_val = prev ($ array); # value of the previous item $ prev_key = key ($ array); # key of the previous item
Use the next function to obtain the next item. The method is similar.
Reference: http://stackoverflow.com/questions/47...
Traverse the array and then use the prev/next functions, or extract keys to form a new array. Then, use value (the original key value) to obtain the numerical subscript and then correspond to the key.
In general, there seems to be no good way to do this.
The methods mentioned above all need to repeat the entire array, and the efficiency is relatively low. Due to the language level, only the php language can be used.
However, as answered by @ liruqi, PHP uses the HASH + two-way linked list method. Therefore, if you write a C extension for PHP, you can use O (1) to obtain the answer.