|
current
Return the current element in an array
(PHP 4, PHP 5)
Example 301. Example use of current() and friends<?php Related Examples ( Source code ) » current Examples ( Source code ) » Cookie based login form and get last login time Examples ( Source code ) » SAX parser in action Examples ( Source code ) » Turning an XML Document into an Array Examples ( Source code ) » Generating Tables from XML Examples ( Source code ) » Transforming XML into Basic HTML Examples ( Source code ) » Adding a Row to a Table Examples ( Source code ) » Listing All Rows and Fields in a Table Examples ( Source code ) » Finding the Number of Rows Returned by a SELECT Statement with mysql_num_rows() Examples ( Source code ) » The current working directory Examples ( Source code ) » Inheriting a Shape Class Examples ( Source code ) » Improved Inheritance Examples ( Source code ) » Apache Variables Examples ( Source code ) » Get date and time Examples ( Source code ) » Display the current locale information and change the locale Examples ( Source code ) » Number format for current Code Examples / Notes » currentvaclav 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 ¤t_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 Languagearray_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 |