无需首先遍历数组就可以设置
PHP的内部数组指针.以下面的虚拟代码为例:
- $array = range(1,100);
- // Represents the array key
- $pointer = 66;
- set_array_pointer($pointer,$array);
- $nextValue = next($array); // Should return 68
使用
ArrayIterator::seek提供的LibertyPaul解决方案似乎是使PHP设置指针到数组中的位置的唯一方法,而不会初始化用户界面中的循环.
尽管如此,您可以从ArrayIterator :: seek()的PHP source中读取,PHP将内部循环通过数组来设置指针:
尽管如此,您可以从ArrayIterator :: seek()的PHP source中读取,PHP将内部循环通过数组来设置指针:
- /* {{{ proto void ArrayIterator::seek(int $position)
- Seek to position. */
- SPL_METHOD(Array,seek)
- {
- zend_long opos,position;
- zval *object = getThis();
- spl_array_object *intern = Z_SPLARRAY_P(object);
- HashTable *aht = spl_array_get_hash_table(intern);
- int result;
- if (zend_parse_parameters(ZEND_NUM_ARGS(),"l",&position) == FAILURE) {
- return;
- }
- if (!aht) {
- PHP_error_docref(NULL,E_NOTICE,"Array was modified outside object and is no longer an array");
- return;
- }
- opos = position;
- if (position >= 0) { /* negative values are not supported */
- spl_array_rewind(intern);
- result = SUCCESS;
- while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
- if (result == SUCCESS && zend_hash_has_more_elements_ex(aht,spl_array_get_pos_ptr(aht,intern)) == SUCCESS) {
- return; /* ok */
- }
- }
- zend_throw_exception_ex(spl_ce_OutOfBoundsException,"Seek position %pd is out of range",opos);
- } /* }}} */
所以看起来没有办法将数组指针设置到某个位置,而不会循环遍历数组