2024 年 PHP 日本研討會

current

(PHP 4, PHP 5, PHP 7, PHP 8)

current傳回陣列中的目前元素

說明

current(陣列|物件 $array): 混合

每個陣列都有一個指向其「目前」元素的內部指標,該指標初始化為插入陣列的第一個元素。

參數

array

該陣列。

傳回值

current() 函式會傳回內部指標目前指向的陣列元素值。它不會以任何方式移動指標。如果內部指標指向元素列表的末尾之後,或者陣列是空的,current() 會傳回 false

警告

此函式可能傳回布林值 false,但也可能傳回評估為 false 的非布林值。請閱讀 布林值 的章節以了解更多資訊。使用 === 運算子 來測試此函式的傳回值。

更新日誌

版本 說明
8.1.0 物件 呼叫此函式已被棄用。請先使用 get_mangled_object_vars()物件 轉換為 陣列,或者使用實作 Iterator 的類別所提供的方法,例如 ArrayIterator
7.4.0 SPL 類別的實例現在會被視為沒有屬性的空物件,而不是呼叫與此函式同名的 Iterator 方法。

範例

範例 #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) { }
?>

注意事項

注意在空陣列和內部指標指向元素末尾之後的陣列上呼叫 current() 的結果,與 布林值 false 元素無法區分。要正確遍歷可能包含 false 元素的陣列,請參閱 foreach 控制結構。 若要仍然使用 current() 並正確檢查該值是否確實是陣列的元素,則應檢查 current() 元素的 key() 是否與 null 嚴格不同。

另請參閱

  • end() - 將陣列的內部指標設定為其最後一個元素
  • key() - 從陣列擷取鍵名
  • each() - 從陣列返回目前的鍵/值對,並將陣列游標往前移動
  • prev() - 將內部陣列指標倒回
  • reset() - 將陣列的內部指標設定為其第一個元素
  • next() - 將陣列的內部指標往前移動
  • foreach

新增註釋

使用者貢獻的註釋 13 則註釋

8
sergey dot karavay at gmail dot com
3 年前
看起來從 PHP 7.4 開始,對物件呼叫 `current()` 已被棄用。

考慮以下程式碼

```
$a = new ArrayIterator([1,2,3]);

var_dump(current($a), $a->current());
```

它會返回
```
int(1)
int(1)
```

在 PHP 7.3 中,但在 PHP 7.4 中,您會得到
```
bool(false)
int(1)
```

而在 PHP 8 中
```
已棄用:current():在 /in/fdrNR 的第 5 行,呼叫 current() 於物件上已被棄用
bool(false)
int(1)
```
20
michael at squiloople dot com
12 年前
current() 也適用於物件

<?php

echo current((object) array('one', 'two')); // 輸出:one

?>
7
strate at yandex dot com
11 年前
請注意,您可以透過運算式傳遞陣列,而不僅僅是透過參考傳遞(如文件中所述)。

<?php
var_dump
( current( array(1,2,3) ) ); // (int) 1
?>
8
vaclav dot sir at gmail dot com
17 年前
關於該「注意」:您將無法區分陣列的末尾和布林值 FALSE 元素,但您可以區分末尾和 key() 函數的 NULL 值。

範例
<?php
if (key($array) === null) {
echo
"您已到達陣列的尾端。";
} else {
echo
"目前元素: " . current($array);
}
?>
8
retestro_REMOVE at SPAM_esperanto dot org dot il
21 年前
文件沒有說明這一點,但使用括號語法將元素添加到陣列
<?php $my_array[] = $new_value; ?>
並不會推進陣列的內部指標。因此,您無法使用 current() 來獲取最後添加的值,也無法使用 key() 來獲取最近添加元素的鍵值。

您應該執行 end($my_array) 將內部指標推進到尾端(如 end() 的其中一個註釋所述),然後

<?php
$last_key
= key($my_array); // 將返回鍵值
$last_value = current($my_array); // 將返回值
?>

如果您不需要鍵值,$last_value = end($my_array) 也可以完成此工作。

- Sergey.
6
mdeng at kabenresearch dot com
20 年前
對於大型陣列(我的範例是 80000 多個元素),如果您想依序遍歷陣列,使用陣列索引 $a[$i] 的效率可能非常低(非常慢)。我不得不改用 current($a)。
5
Vasily Yudin (st-2 at mail dot ru)
10 年前
如果您在 foreach 迴圈中使用 unset() 後執行 current(),在 PHP 5.2.4 及更高版本中可能會得到 FALSE。
以下是一個例子
<?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)
?>
如果您在沒有 foreach 的情況下使用 unset(),一切都會正常。
<?php
$prices
= array(
0 => '1300990',
1 => '500',
2 => '600'
);
unset(
$prices[1]);
unset(
$prices[2]);

var_dump(current($prices));
?>
1
leozmm at outlook dot com
5 年前
在 `current` 函式中,陣列可以透過「引用」和「表達式」兩種方式傳遞,因為 `current` 不會移動陣列的內部指標。
這不適用於其他函式,例如 `end`、`next`、`prev` 等。

<?php
function foo() {return array(1,2,3);}
echo
current(foo()); // 輸出 '1'
echo end(foo()); // 發生錯誤:只能以引用方式傳遞變數
?>
1
xedin dot unknown at gmail dot com
6 年前
像 `current()` 和 `rewind()` 這樣的陣列函式也適用於 `Traversable`,PHP 5.0 - 7.3 版本皆適用,但不適用於 HHVM。

<?php

$queue
= new ArrayIterator(array('adasdasd'));
reset($queue);
$current = current($queue);
var_dump($current);

?>

參考 https://3v4l.org/VjCHR
2
vitalib at 012 dot net dot il
21 年前
請注意,複製陣列時會遺失其內部指標。

<?php
$myarray
= array(0=>'a', 1=>'b', 2=>'c');
next($myarray);
print_r(current($myarray));
echo
'<br>';
$a = $myarray;
print_r(current($a));
?>

會先輸出 'b',然後輸出 'a',因為內部指標沒有被複製。您可以使用引用來解決這個問題,如下所示:

<?php
$a
=& $myarray;
?>
0
pdarmis at gmail dot com
7 年前
基於這個例子 https://php.dev.org.tw/manual/en/function.current.php#116128 我想補充以下說明。 正如 Vasily 在他的例子中指出的
<?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)
?>
上述例子在 PHP 5.2.4 到 5.6.29 版本之間無法正常運作,會回傳 false。 這個問題在 PHP 7.0.1 以上版本則不存在。
另一個解決方法(至少與 Vasily 的例子不同)是在使用 current() 之前使用 reset(),以便將陣列指標重設到起始位置。
<?php
$prices
= array(
0 => '1300990',
1 => '500',
2 => '600'
);
foreach(
$prices as $key => $price){
if(
$price < 1000){
unset(
$prices[$key]);
}
}
reset($prices);
var_dump(current($prices)); // string(7) "1300990"
?>
0
gregory at gregory dot net
16 年前
我花了一段時間才弄清楚這一點,但有一種比使用 each() 更一致的方法來確定是否真的已經超過陣列的結尾。

如您所見,each() 會在推進指標「之前」取得值,而 next() 會在推進指標「之後」取得值。 因此,在實作 Iterator 介面時,使用 each() 真的非常麻煩。

因此,我提供您這個解決方案
要查看是否已超過陣列的結尾,請使用 key($array) 並查看它是否返回 NULL。 如果是,則表示您已超過陣列的結尾——陣列中的鍵不能為 null。

很巧妙,對吧?以下是我如何在其中一個類別中實作 Iterator 介面的方法

<?php

/**
* DbRow file
* @package PalDb
*/

/**
* This class lets you use Db rows and object-relational mapping functionality.
*/

class DbRow implements Iterator
{
/**
* The DbResult object that gave us this row through fetchDbRows
* @var DbResult
*/
protected $result;

/**
* The fields of the row
* @var $fields
*/
protected $fields;

/**
* Constructor
*
* @param PDOStatement $stmt
* The PDO statement object that this result uses
* @param DbResult $result
* The result that produced this row through fetchDbRows
*/
function __construct($result)
{
$this->result = $result;
}

/**
* Get the DbResult object that gave us this row through fetchDbRows
* @return DbResult
*
* @return unknown
*/
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;
}

/**
* Iterator implementation - rewind
*/
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.
-4
Pratip Ghosh
10 年前
如果我們從陣列中取消設定任何元素,然後嘗試使用 current 函式,我注意到它會返回 FALSE。要克服這個限制,您可以使用 array_values 函式重新排序樹狀結構(陣列)。
To Top