好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PHP中array_pop, array_shift使用笔记

在standard/array.c中我们可以找到 array_pop, array_shift这2个函数的C实现 mixed array_pop ( array &array ) array_pop() 弹出并返回 array 数组的最后一个单元,并将数组 array 的长度减一。如果 array 为空(或者不是数组)将返回 NULL 注意: 使用 本函数后会重置(reset())数组指针 mixed array_shift ( array &array ) array_shift() 将 array 的第一个单元移出并作为结果返回,将 array 的长度减一并将所有其它单元向前移动一位。所有的数字键名将改为从零开始计数,文字键名将不变。如果 array 为空(或者不是数组),则返回 NULL。

注意: 使用本函数后会重置(reset())数组指针 这两个函数在实现上都是使用的

/* {{{ proto mixed array_pop(array stack) Pops an element off the end of the array */ PHP_FUNCTION(array_pop) { _phpi_pop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); } /* }}} */ /* {{{ proto mixed array_shift(array stack) Pops an element off the beginning of the array */ PHP_FUNCTION(array_shift) { _phpi_pop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); }

程序首先判断输入,然后判断数组中是否有元素,如果数组为空直接返回, 如果是array_pop: ==>zend_hash_internal_pointer_end ==>zend_hash_internal_pointer_end_ex(ht, NULL) 此时直接返回hashtable中双向链表的最后一个元素 ht->pInternalPointer = ht->pListTail; 如果是array_shift: ==>zend_hash_internal_pointer_reset(Z_ARRVAL_PP(stack)); ==>zend_hash_internal_pointer_reset_ex(ht, NULL) 此时直接返回hashtable中双向链表的第一个元素 ht->pInternalPointer = ht->pListHead; 得到返回值,通过 zend_hash_get_current_data ==> zend_hash_get_current_data_ex(ht, pData, NULL) p = pos ? (*pos) : ht->pInternalPointer; *pData = p->pData;

取得hashtable中的值 然后删除hashtable中的这个key值,并调用zend_hash_internal_pointer_reset重置hashtable 这个重置就是:ht->pInternalPointer = ht->pListHead; 即把当前的位置设置为链表的第一个元素。

查看更多关于PHP中array_pop, array_shift使用笔记的详细内容...

  阅读:38次