亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我要讓指針遍歷數組,得到如下結果該怎么做呢?

我要讓指針遍歷數組,得到如下結果該怎么做呢?

PHP
慕的地6264312 2023-04-16 18:14:44
php的數組都有一個內部指針,指向數組的元素,初始化的時候是第一個,我要便利數組,讓內部指針逐個移動$arr = array ('a', 'b', 'c', 'd', 'e');foreach ($arr as $k => $v) {    $curr = current($arr);    echo "{$k} => {$v} -- {$curr}\n"; }得到結果是0 => a -- b1 => b -- b2 => c -- b3 => d -- b4 => e -- b內部指針向后移動了一位就再也沒動過了。foreach對這個數組做了什么呢?為什么呢?0 => a -- a1 => b -- b2 => c -- c3 => d -- d4 => e -- e
查看完整描述

3 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

1. at the beginning of foreach: ZEND_FE_RESET which increase the refoucnt of $a
2. then FE_FETCH, reset internal pointer of $a
3. then current, current declared to accept a reference, but $a is not a ref and refcount > 1 , then -> separation

查看完整回答
反對 回復 2023-04-21
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

foreach() 操作原始數組的一個拷貝,如果需要移動指針,使用 while 結構加上 each() 來實現。

$arr = array ('a', 'b', 'c', 'd', 'e');reset($arr);while (list($k, $v) = each($arr)) {    # 當前指針已經被指向了下一位
    $curr = current($arr);
    echo "{$k} => {$v} -- {$curr}\n";
}


查看完整回答
反對 回復 2023-04-21
?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

php的所有變量實際上是用一個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;
};

而數組就是其中的"HashTable *ht",實際上就是一個哈希表(Hash Table),表中的所有元素同時又組成一個雙向鏈表,它的定義為:

/* 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;

這里有一個 Bucket *pInternalPointer ,就是被reset/current/next等函數用來遍歷數組保存位置狀態的。Bucket的實現如下,可以看到這就是個赤裸裸的鏈表節點。

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的實現,則位于 ./Zend/zend_compile.h ,在解釋期被flex翻譯成由 zend_do_foreach_begin zend_do_foreach_cont zend_do_foreach_end 這三個函數(以及相關代碼)組合起來。


查看完整回答
反對 回復 2023-04-21
  • 3 回答
  • 0 關注
  • 153 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號