php如何移动数组内部指针

php中文网
发布: 2016-06-06 20:51:09
原创
1337人浏览过

php的数组都有一个内部指针,指向数组的元素,初始化的时候是第一个,我要便利数组,让内部指针逐个移动

$arr = array ('a', 'b', 'c', 'd', 'e');
foreach ($arr as $k => $v) {
    $curr = current($arr);
    echo "{$k} => {$v} -- {$curr}\n";
}
登录后复制

得到结果是

0 => a -- b
1 => b -- b
2 => c -- b
3 => d -- b
4 => e -- b
登录后复制

内部指针向后移动了一位就再也没动过了。。。
foreach对这个数组做了什么呢?为什么呢?
我要让指针遍历数组,得到如下结果改怎么做呢?

0 => a -- a
1 => b -- b
2 => c -- c
3 => d -- d
4 => e -- e
登录后复制

回复内容:

php的数组都有一个内部指针,指向数组的元素,初始化的时候是第一个,我要便利数组,让内部指针逐个移动

$arr = array ('a', 'b', 'c', 'd', 'e');
foreach ($arr as $k => $v) {
    $curr = current($arr);
    echo "{$k} => {$v} -- {$curr}\n";
}
登录后复制

得到结果是

0 => a -- b
1 => b -- b
2 => c -- b
3 => d -- b
4 => e -- b
登录后复制

内部指针向后移动了一位就再也没动过了。。。
foreach对这个数组做了什么呢?为什么呢?
我要让指针遍历数组,得到如下结果改怎么做呢?

0 => a -- a
1 => b -- b
2 => c -- c
3 => d -- d
4 => e -- e
登录后复制

这个问题我在bugs.php.net上回答过: https://bugs.php.net/bug.php?id=63752

立即学习PHP免费学习笔记(深入)”;

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

PHP 的数组指针操作函数:

  1. pos() http://cn2.php.net/manual/en/function...
  2. end() http://cn2.php.net/manual/en/function...
  3. prev() http://cn2.php.net/manual/en/function...
  4. next() http://cn2.php.net/manual/en/function...
  5. each() http://cn2.php.net/manual/en/function...
  6. reset() http://cn2.php.net/manual/en/function...
  7. 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() 操作原始数组的一个拷贝,如果需要移动指针,使用 while 结构加上 each() 来实现。

动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版
动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版

动态WEB网站中的PHP和MySQL详细反映实际程序的需求,仔细地探讨外部数据的验证(例如信用卡卡号的格式)、用户登录以及如何使用模板建立网页的标准外观。动态WEB网站中的PHP和MySQL的内容不仅仅是这些。书中还提到如何串联JavaScript与PHP让用户操作时更快、更方便。还有正确处理用户输入错误的方法,让网站看起来更专业。另外还引入大量来自PEAR外挂函数库的强大功能,对常用的、强大的包

动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版 525
查看详情 动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版
$arr = array ('a', 'b', 'c', 'd', 'e');
reset($arr);
while (list($k, $v) = each($arr)) {
    # 当前指针已经被指向了下一位
    $curr = current($arr);
    echo "{$k} => {$v} -- {$curr}\n";
}
登录后复制

参见 http://cn2.php.net/manual/en/function... 的 example #2。

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 这三个函数(以及相关代码)组合起来。由于看起来比较晦涩,我就不贴出来了(实际上我也没看太懂),详情可以参考雪候鸟的这篇:深入理解PHP原理之foreach

最后附一段php代码的opcode

<?php
$arr = array(1,2,3);
foreach ($arr as $x) 
    echo $x;  
?>
登录后复制
number of ops:  12
compiled vars:  !0 = $arr, !1 = $x
line  # *  op                   return  operands
-----------------------------------------------------
2     0  >   INIT_ARRAY           ~0      1         用1初始化数组
      1      ADD_ARRAY_ELEMENT    ~0      2         添加个2
      2      ADD_ARRAY_ELEMENT    ~0      3         添加个3
      3      ASSIGN                       !0, ~0    存入$arr
3     4    > FE_RESET             $2      !0, ->10  $2 = FE_RESET($arr), 失败则跳到#10
      5  > > FE_FETCH             $3      $2, ->10  $3 = FE_FETCH($2), 失败则跳到#10
      6  >   ZEND_OP_DATA                           
      7      ASSIGN                       !1, $3    $x = $3
4     8      ECHO                         !1        echo $x
      9    > JMP                          ->5       跳到#5
     10  >   SWITCH_FREE                  $2        释放$2
5    11    > RETURN                       1         返回
登录后复制
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号