百科狗-知识改变命运!
--

current() - 返回数组中的当前单元 - php 数组函数

是丫丫呀1年前 (2023-11-21)阅读数 13#技术干货
文章标签数组

current()

(PHP 4, PHP 5, PHP 7)

current() - 返回数组中的当前单元 - php 数组函数

返回数组中的当前单元

说明

current(array &$array): mixed

每个数组中都有一个内部的指针指向它“当前的”单元,初始指向插入到数组中的第一个单元。

参数

$array

这个数组。

返回值

current()函数返回当前被内部指针指向的数组单元的值,并不移动指针。如果内部指针指向超出了单元列表的末端,current()返回FALSE

Warning

此函数可能返回布尔值FALSE,但也可能返回等同于FALSE的非布尔值。请阅读布尔类型章节以获取更多信息。应使用===运算符来测试此函数的返回值。

范例

使用current()系列函数的例子

注释

Note:如果数组包含booleanFALSE的单元则本函数在碰到这个单元时也返回FALSE,使得不可能判断是否到了此数组列表的末端。要正确遍历可能含有空单元的数组,用each()函数。

参见

  • end() 将数组的内部指针指向最后一个单元
  • key() 从关联数组中取得键名
  • each() 返回数组中当前的键/值对并将数组指针向前移动一步
  • prev() 将数组的内部指针倒回一位
  • reset() 将数组的内部指针指向第一个单元
  • next() 将数组中的内部指针向前移动一位
current() also works on objects: 
Note, that you can pass array by expression, not only by reference (as described in doc). 
If you do current() after using uset() on foreach statement, you can get FALSE in PHP version 5.2.4 and above.
There is example:

If you do unset() without foreach? all will be fine. 
Array functions, such as `current()` and `rewind()` will work on `Traversable` as well, PHP 5.0 - 7.3, but not in HHVM:

See https://3v4l.org/VjCHR
For large array(my sample was 80000+ elements), if you want to traverse the array in sequence, using array index $a[$i] could be very inefficient(very slow). I had to switch to use current($a).
To that "note": You won't be able to distinguish the end of an array from a boolean FALSE element, BUT you can distinguish the end from a NULL value of the key() function.
Example: 
Note that by copying an array its internal pointer is lost:

Would output 'b' and then 'a' since the internal pointer wasn't copied. You can cope with that problem using references instead, like that: 
The docs do not specify this, but adding to the array using the brackets syntax:
   
will not advance the internal pointer of the array. therefore, you cannot use current() to get the last value added or key() to get the key of the most recently added element.
You should do an end($my_array) to advance the internal pointer to the end ( as stated in one of the notes on end() ), then
  
If you have no need in the key, $last_value = end($my_array) will also do the job.
- Sergey.
Array can be passed by both REFERENCE and EXPRESSION on `current`, because current doesn't move array's internal pointer,
this is not true for other functions like: `end`, `next`, `prev` etc. 
Based on this example http://php.net/manual/en/function.current.php#116128 i would like to add the following. As Vasily points out in his example 

The above example will not work and return false for version of PHP between 5.2.4 and 5.6.29. The issue is not present on PHP versions >= 7.0.1
A different workaround (at least from Vasily's example) would be to use reset() before using current() in order to reset the array pointer to start. 
It took me a while to figure this out, but there is a more consistent way to figure out whether you really went past the end of the array, than using each().
You see, each() gets the value BEFORE advancing the pointer, and next() gets the value AFTER advancing the pointer. When you are implementing the Iterator interface, therefore, it's a real pain in the behind to use each().
And thus, I give you the solution:
To see if you've blown past the end of the array, use key($array) and see if it returns NULL. If it does, you're past the end of the array -- keys can't be null in arrays.
Nifty, huh? Here's how I implemented the Iterator interface in one of my classes:

That's all ... bye.
If we unset any element from an array, and then try the current function, I noted it returned FALSE. To overcome this limitation, you can use array_values function to re-order the tree.
As Sergey stated a long time ago in this notes, when adding to the array using the brackets syntax, current won't return the last added value. Instead of moving the pointer with end(), you simply can get the last value with  (only applies to a non-associative array).

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)