©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 4, PHP 5, PHP 7)
current — 返回数组中的当前单元
&$array
)每个数组中都有一个内部的指针指向它“当前的”单元,初始指向插入到数组中的第一个单元。
array
这个数组。
current() 函数返回当前被内部指针指向的数组单元的值,并不移动指针。如果内部指针指向超出了单元列表的末端, current()
返回 FALSE
。
此函数可能返回布尔值
FALSE
,但也可能返回等同于 FALSE
的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用
===
运算符来测试此函数的返回值。
Example #1 使用 current() 系列函数的例子
<?php
$transport = array( 'foot' , 'bike' , 'car' , 'plane' );
$mode = current ( $transport ); // $mode = 'foot';
$mode = next ( $transport ); // $mode = 'bike';
$mode = current ( $transport ); // $mode = 'bike';
$mode = prev ( $transport ); // $mode = 'foot';
$mode = end ( $transport ); // $mode = 'plane';
$mode = current ( $transport ); // $mode = 'plane';
$arr = array();
var_dump ( current ( $arr )); // bool(false)
$arr = array(array());
var_dump ( current ( $arr )); // array(0) { }
?>
Note: 如果数组包含 boolean
FALSE
的单元则本函数在碰到这个单元时也返回FALSE
,使得不可能判断是否到了此数组列表的末端。 要正确遍历可能含有空单元的数组,用 each() 函数。
[#1] Vasily Yudin (st-2 at mail dot ru) [2014-11-12 08:51:11]
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:
<?php
$prices = array(
0 => '1300990',
1 => '500',
2 => '600'
);
foreach($prices as $key => $price){
if($price < 1000){
unset($prices[$key]);
}
}
var_dump(current($prices)); // bool(false)
?>
If you do unset() without foreach? all will be fine.
<?php
$prices = array(
0 => '1300990',
1 => '500',
2 => '600'
);
unset($prices[1]);
unset($prices[2]);
var_dump(current($prices));
?>
[#2] Pratip Ghosh [2014-05-14 05:55:07]
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.
[#3] strate at yandex dot com [2013-10-30 07:11:28]
Note, that you can pass array by expression, not only by reference (as described in doc).
<?php
var_dump( current( array(1,2,3) ) ); // (int) 1
?>
[#4] michael at squiloople dot com [2012-01-10 19:44:59]
current() also works on objects:
<?php
echo current((object) array('one', 'two')); // Outputs: one
?>
[#5] aefxx [2008-05-15 08:39:40]
A simple copy function that not only copies the given array but ensures the copy's pointer is set to the exact same position:
<?php
function array_copy(&array)
{
$key = key($array);
$copy = $array;
while (($copy_key = key($copy)) !== NULL) {
if ($copy_key == $key) break;
next($copy);
}
return $copy;
}
?>
That's all ... bye.
[#6] gregory at gregory dot net [2008-02-27 21:07:34]
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:
<?php
class DbRow implements Iterator
{
protected $result;
protected $fields;
function __construct($result)
{
$this->result = $result;
}
function getResult()
{
return $this->result;
}
function __set(
$name,
$value)
{
$this->fields[$name] = $value;
}
function __get(
$name)
{
if (isset($this->fields[$name]))
return $this->fields[$name];
else
return null;
}
function rewind()
{
$this->beyondLastField = false;
return reset($this->fields);
}
function valid()
{
return !$this->beyondLastField;
}
function current()
{
return current($this->fields);
}
function key()
{
return key($this->fields);
}
function next()
{
$next = next($this->fields);
$key = key($this->fields);
if (isset($key)) {
return $next[1];
} else {
$this->beyondLastField = true;
return false; // doesn't matter what we return here, see valid()
}
}
private $beyondLastField = false;
};
Hope this helps someone.
[#7] vaclav dot sir at gmail dot com [2007-08-13 09:23:37]
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:
<?php
if (key($array) === null) {
echo "You are in the end of the array.";
} else {
echo "Current element: " . current($array);
}
?>
[#8] mdeng at kabenresearch dot com [2004-04-23 23:04:00]
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).
[#9] vitalib at 012 dot net dot il [2003-12-02 02:10:00]
Note that by copying an array its internal pointer is lost:
<?php
$myarray = array(0=>'a', 1=>'b', 2=>'c');
next($myarray);
print_r(current($myarray));
echo '<br>';
$a = $myarray;
print_r(current($a));
?>
Would output 'b' and then 'a' since the internal pointer wasn't copied. You can cope with that problem using references instead, like that:
<?php
$a =& $myarray;
?>
[#10] retestro_REMOVE at SPAM_esperanto dot org dot il [2003-03-01 18:31:33]
The docs do not specify this, but adding to the array using the brackets syntax:
<?php $my_array[] = $new_value; ?>
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
<?php
$last_key = key($my_array); // will return the key
$last_value = current($my_array); // will return the value
?>
If you have no need in the key, $last_value = end($my_array) will also do the job.
- Sergey.