Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Array Functions : current

current

Return the current element in an array (PHP 4, PHP 5)
mixed current ( array &array )

Example 301. 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';
?>

Related Examples ( Source code ) » current
















Code Examples / Notes » current

vaclav dot sir

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);
}
?>


marnaq

To make this function return a reference to the element instead, use:
<?php
function &current_by_ref(&$arr) {
   return $arr[key($arr)];
}
?>


retestro_remove

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.


vitalib

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

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 :)


mdeng

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

Change Language


Follow Navioo On Twitter
array_change_key_case
array_chunk
array_combine
array_count_values
array_diff_assoc
array_diff_key
array_diff_uassoc
array_diff_ukey
array_diff
array_fill_keys
array_fill
array_filter
array_flip
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
array_key_exists
array_keys
array_map
array_merge_recursive
array_merge
array_multisort
array_pad
array_pop
array_product
array_push
array_rand
array_reduce
array_reverse
array_search
array_shift
array_slice
array_splice
array_sum
array_udiff_assoc
array_udiff_uassoc
array_udiff
array_uintersect_assoc
array_uintersect_uassoc
array_uintersect
array_unique
array_unshift
array_values
array_walk_recursive
array_walk
array
arsort
asort
compact
count
current
each
end
extract
in_array
key
krsort
ksort
list
natcasesort
natsort
next
pos
prev
range
reset
rsort
shuffle
sizeof
sort
uasort
uksort
usort
eXTReMe Tracker