search for in the  
<counteach>
Last updated: Thu, 19 May 2005

current

(PHP 3, PHP 4, PHP 5)

current -- Return the current element in an array

Description

mixed current ( array &array )

Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array.

The current() function simply returns the value of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list, current() returns FALSE.

Warning

If the array contains empty elements (0 or "", the empty string) then this function will return FALSE for these elements as well. This makes it impossible to determine if you are really at the end of the list in such an array using current(). To properly traverse an array that may contain empty elements, use the each() function.

Example 1. Example use of current() and friends

<?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';
?>

See also end(), key(), next(), prev(), and reset().



User Contributed Notes
current
jari dot eskelinen at iki dot fi
02-Dec-2004 07:08
mattblisset's array_set_current has one flaw: if you have array with key 0, while-loop will stop there. Fixed version:

function array_set_current(&$array, $key){
   reset($array);
   while (current($array)!==FALSE){
       if (key($array) == $key) {
           break;
       }
       next($array);
   }
   return current($array);
}
mdeng at kabenresearch dot com
24-Apr-2004 01: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).
mattblissett at hotmail dot com
01-Jan-2004 04:36
Sam said:
"Here is a function that sets the current pointer of an array to a given key."

If your array pointer doesn't start at the beginning, this won't work for moving the pointer to a lower one. So, use a reset() first:

<?php
//set the pointer of an array to the given key
function array_set_current(&$array, $key){
  
reset($array);
   while(
current($array)){ 
       if(
key($array) == $key){
           break;
       }
      
next($array);
   }
}
?>

HTH,
Matt
vitalib at 012 dot net dot il
02-Dec-2003 04: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;
?>
jdpipe at yahoo dot co dot uk
24-Nov-2003 11:08
You can't get a pointer to an element in an array using the 'current' construct, for example:

<?php

$a
=array();

$a[]='aaa';
$a[]='bbb';
$a[]='ccc';
$a[]='ddd';
$a[]='eee';

reset($a);
next($a);
$v =& current($a);
$v = 'xxxx';

// array is not changed
print_r($a);

next($a);

$v =& $a[key($a)];
$v = 'yyy';

// now the array is changed
print_r($a);

?>
si
15-Sep-2003 12:57
current isn't able to return by reference, eg.

function &getDataPacket()
{
global $globalref;
return current( $globalref[ key( $globalref ) ]->dp ); // *FAILS*
return $globalref[ key( $globalref ) ]->dp[ key( $globalref[ key( $globalref ) ]->dp ) ]; // *WORKS*
}

According to Bug#12692 it's intentional, so watch out!
tipman
08-May-2003 06: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 :)
kyle
17-Mar-2003 05:49
The comment by 'someone out there' is wrong according to the warning.  If I read the warning right, if there are elements with empty values, it will die. 

look at array_keys, it does what is described there, and probably faster too.  Then loop through that (since all numeric indices, simple for loop should work:

$keys=array_keys($myarr);
for($x=0;$x<count($keys);$x++) {
   ...
}

though foreach is probably recommended)
php4all at free dot fr
12-Mar-2003 09:37
There is an other way to grag the key and the value of an Array:

<?
foreach($myArray as $Key => $Value) {
   echo
"Key:" . $Key;
   echo
"Value:" . $Value
}
?>

Then you will have the key and the value of each element in the Array.

php4all
someone out there
11-Mar-2003 04:42
here's a way to grab array keys:

reset($myArray);

while (current($myArray) !== false) {
   $myKey = key($myArray);
   echo $myKey; //do as you will with each key
   next($myArray);
}
retestro_REMOVE at SPAM_esperanto dot org dot il
01-Mar-2003 08: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.

<counteach>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT