Php arrays all have an internal pointer pointing to the elements of the array, which is the first during initialization. I want to facilitate the array and move the internal pointer one by one {code ...} the result is {code ...} the internal pointer is moved backward and never again... What does foreach do to this array... php arrays all have an internal pointer pointing to the elements of the array, which is the first during initialization. I want to facilitate the array and move the internal pointers one by one
$arr = array ('a', 'b', 'c', 'd', 'e');foreach ($arr as $k => $v) { $curr = current($arr); echo "{$k} => {$v} -- {$curr}\n";}
The result is:
0 => a -- b1 => b -- b2 => c -- b3 => d -- b4 => e -- b
The internal pointer is moved backward and never again...
What does foreach do to this array? Why?
I want the pointer to traverse the array. How can I change the result as follows?
0 => a -- a1 => b -- b2 => c -- c3 => d -- d4 => e -- e
Reply content:
Php arrays all have an internal pointer pointing to the elements of the array, which is the first during initialization. I want to facilitate the array and move the internal pointers one by one
$arr = array ('a', 'b', 'c', 'd', 'e');foreach ($arr as $k => $v) { $curr = current($arr); echo "{$k} => {$v} -- {$curr}\n";}
The result is:
0 => a -- b1 => b -- b2 => c -- b3 => d -- b4 => e -- b
The internal pointer is moved backward and never again...
What does foreach do to this array? Why?
I want the pointer to traverse the array. How can I change the result as follows?
0 => a -- a1 => b -- b2 => c -- c3 => d -- d4 => e -- e
This question I answered on bugs.php.net: https://bugs.php.net/bug.php? Id = 63752
1. at the beginning of foreach: ZEND_FE_RESET which increase the refoucnt of $
2. then FE_FETCH, reset internal pointer of $
3. then current, current declared to accept a reference, but $ a is not a ref and refcount> 1, then-> separation
PHP array pointer operation functions:
pos()
Http://cn2.php.net/manual/en/function...
end()
Http://cn2.php.net/manual/en/function...
prev()
Http://cn2.php.net/manual/en/function...
next()
Http://cn2.php.net/manual/en/function...
each()
Http://cn2.php.net/manual/en/function...
reset()
Http://cn2.php.net/manual/en/function...
current()
Http://cn2.php.net/manual/en/function...
"Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the each () construct and changes to the array element returned are not reflected in the original array."
Http://cn2.php.net/manual/en/control -...
foreach()
Operate a copy of the original array. To move the pointer, usewhile
Structureeach()
.
$ Arr = array ('A', 'B', 'C', 'D', 'E'); reset ($ arr); while (list ($ k, $ v) = each ($ arr) {# The current pointer has been directed to the next position $ curr = current ($ arr ); echo "{$ k }=>{$ v} -- {$ curr} \ n ";}
See example #2 in http://cn2.php.net/manual/en/function.
All php variables are actually represented by a struct zval.
/* Zend/zend.h */typedef struct _zval_struct zval;typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object_value obj;} zvalue_value;struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uint refcount; zend_uchar type; /* active type */ zend_uchar is_ref;};
The array is the "HashTable * ht", which is actually a Hash Table. All the elements in the Table form a two-way linked list at the same time. Its definition is as follows:
/* 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_DEBUG int inconsistent;#endif} HashTable;
Here is a Bucket * pInternalPointer, which is used by reset/current/next and other functions to traverse the array and save the Location status. The Bucket implementation is as follows. We can see that this is a naked linked list node.
typedef struct bucket { ulong h; /* Used for numeric indexing */ uint nKeyLength; void *pData; void *pDataPtr; struct bucket *pListNext; struct bucket *pListLast; struct bucket *pNext; struct bucket *pLast; char arKey[1]; /* Must be last element */} Bucket;
Foreach is implemented in./Zend/zend_compile.h. During the interpretation period, flex translates it into three functions (and related code), zend_do_foreach_forein zend_do_foreach_end. Because it looks obscure, I will not post it out (I didn't understand it too much). For details, refer to this article of snow migratory birds: Understanding the principles of PHP foreach
The opcode of the last php code
Number of ops: 12 compiled vars :! 0 = $ arr ,! 1 = $ xline # * op return operands ----------------------------------------------------- 2 0> INIT_ARRAY ~ 0 1 initialize array 1 ADD_ARRAY_ELEMENT ~ with 1 ~ 0 2 Add 2 2 ADD_ARRAY_ELEMENT ~ 0 3 add 3 3 ASSIGN! 0 ,~ 0 saved $ arr3 4> FE_RESET $2! 0,-> 10 $2 = FE_RESET ($ arr), skip to #10 5> FE_FETCH $3 $2,-> 10 $3 = FE_FETCH ($2 ), if it fails, it will jump to #10 6> ZEND_OP_DATA 7 ASSIGN! 1, $3 $ x = $34 8 ECHO! 1 echo $ x 9> JMP-> 5 jump to #5 10> SWITCH_FREE $2 release $25 11> RETURN 1 RETURN