设置内部数组指针,不用PHP迭代
发布时间:2020-05-25 10:07:12 所属栏目:PHP 来源:互联网
导读:无需首先遍历数组就可以设置 PHP的内部数组指针.以下面的虚拟代码为例: $array = range(1, 100);// Represents the array key$pointer = 66;set_array_pointer($pointer, $array);$nextValue = next($array); // Should return 68 使用 ArrayI
|
无需首先遍历数组就可以设置 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将内部循环通过数组来设置指针: /* {{{ 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);
} /* }}} */
所以看起来没有办法将数组指针设置到某个位置,而不会循环遍历数组 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
