设置内部数组指针,不用PHP迭代

前端之家收集整理的这篇文章主要介绍了设置内部数组指针,不用PHP迭代前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
无需首先遍历数组就可以设置 PHP的内部数组指针.以下面的虚拟代码为例:
  1. $array = range(1,100);
  2.  
  3. // Represents the array key
  4. $pointer = 66;
  5.  
  6. set_array_pointer($pointer,$array);
  7.  
  8. $nextValue = next($array); // Should return 68
使用 ArrayIterator::seek提供的LibertyPaul解决方案似乎是使PHP设置指针到数组中的位置的唯一方法,而不会初始化用户界面中的循环.
尽管如此,您可以从ArrayIterator :: seek()的PHP source中读取,PHP将内部循环通过数组来设置指针:
  1. /* {{{ proto void ArrayIterator::seek(int $position)
  2. Seek to position. */
  3. SPL_METHOD(Array,seek)
  4. {
  5. zend_long opos,position;
  6. zval *object = getThis();
  7. spl_array_object *intern = Z_SPLARRAY_P(object);
  8. HashTable *aht = spl_array_get_hash_table(intern);
  9. int result;
  10.  
  11. if (zend_parse_parameters(ZEND_NUM_ARGS(),"l",&position) == FAILURE) {
  12. return;
  13. }
  14.  
  15. if (!aht) {
  16. PHP_error_docref(NULL,E_NOTICE,"Array was modified outside object and is no longer an array");
  17. return;
  18. }
  19.  
  20. opos = position;
  21.  
  22. if (position >= 0) { /* negative values are not supported */
  23. spl_array_rewind(intern);
  24. result = SUCCESS;
  25.  
  26. while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
  27.  
  28. if (result == SUCCESS && zend_hash_has_more_elements_ex(aht,spl_array_get_pos_ptr(aht,intern)) == SUCCESS) {
  29. return; /* ok */
  30. }
  31. }
  32. zend_throw_exception_ex(spl_ce_OutOfBoundsException,"Seek position %pd is out of range",opos);
  33. } /* }}} */

所以看起来没有办法将数组指针设置到某个位置,而不会循环遍历数组

猜你在找的PHP相关文章