Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: current - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net

search for in the

each" width="11" height="7"/> <count
Last updated: Mon, 05 Feb 2007
view this page in

current

(PHP 4, PHP 5)

current — 配列内の現在の要素を返す

説明

mixed current ( array &array )

各配列は、"カレント"の要素へのポインタを有しています。 このポインタは、その配列の最初の要素を指すように初期化されます。

current() 関数は、 単に内部ポインタが現在指している配列要素の値を返します。 この関数は、ポインタを全く移動しません。 内部ポインタが最終要素の次を指していた場合、 current()FALSE を返します。

警告
この関数は論理値 FALSE を返す可能性がありますが、FALSE として評価される 0 や "" といった値を返す可能性もあります。 詳細については 論理値の セクションを参照してください。この関数の返り値を調べるには ===演算子 を 使用してください。

注意: 配列中に boolean FALSE の要素が含まれていると、 それを配列の終わりと区別することができません。FALSE 要素を含む配列を順に処理するには、each() 関数を参照ください。

例 280. 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';
?>

end()key()next()prev()reset() および each() も参照ください。



add a note add a note User Contributed Notes
current
marnaq
18-Aug-2006 09:20
To make this function return a reference to the element instead, use:

<?php
function &current_by_ref(&$arr) {
   return
$arr[key($arr)];
}
?>
mdeng at kabenresearch dot com
24-Apr-2004 03:04
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).
vitalib at 012 dot net dot il
02-Dec-2003 07:10
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;
?>
tipman
08-May-2003 08:07
if you got a array with number as index you get the last index with this:

eg:
$array[0] = "foo";
$array[1] = "foo2";

$lastKey = sizeof($array) - 1;

only a little help :)
retestro_REMOVE at SPAM_esperanto dot org dot il
02-Mar-2003 11:31
The docs do not specify this, but adding to the array using the brackets syntax:
     $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

     $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.

each" width="11" height="7"/> <count
Last updated: Mon, 05 Feb 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites