To make this function return a reference to the element instead, use:
<?php
function ¤t_by_ref(&$arr) {
return $arr[key($arr)];
}
?>
current
(PHP 4, PHP 5)
current — Liefert das aktuelle Element eines Arrays
Beschreibung
Jedes Array hat einen internen Zeiger auf sein "aktuelles" Element, welcher auf das erste in das Array eingefügte Element initialisiert wird.
Parameter-Liste
-
array -
Das Array.
Rückgabewerte
Die Funktion current() liefert den Wert des Array
Elements, auf das gerade vom internen Zeiger gezeigt wird. Sie
bewegt den Zeiger in keinster Weise. Zeigt der interne Zeiger
hinter das Ende der Elementenliste, gibt
current() FALSE zurück.
Diese Funktion kann sowohl das
boolsche FALSE zurückliefern, als auch einen nicht-boolschen Wert, welcher zu FALSE ausgewertet wird.
Weitere Informationen entnehmen Sie bitte dem Abschnitt über die
boolschen Typen. Benutzen Sie deshalb
den === Operator,
um den Rückgabewert dieser Funktion zu überprüfen.
Beispiele
Beispiel #1 Beispiel für die Verwendung von current() und anderen
<?php
$transport = array('zu Fuß', 'Fahrrad', 'Auto', 'Flugzeug');
$mode = current($transport); // $mode = 'zu Fuß';
$mode = next($transport); // $mode = 'Fahrrad';
$mode = next($transport); // $mode = 'Auto';
$mode = prev($transport); // $mode = 'Fahrrad';
$mode = end($transport); // $mode = 'Flugzeug';
$arr = array();
var_dump(current($arr)); // bool(false)
$arr = array(array());
var_dump(current($arr)); // array(0) { }
?>
Anmerkungen
Hinweis: Es ist nicht möglich, das Ende eines Arrays von einem boolean
FALSE-Wert zu unterscheiden. Um ein Array, dasFALSE-Elemente beinhalten könnte, korrekt zu durchlaufen werfen Sie bitte einen Blick auf die each()- Funktion.
Siehe auch
- end() - Positioniert den internen Zeiger eines Arrays auf dessen letztes Element
- key() - Liefert einen Schlüssel eines assoziativen Arrays
- each() - Liefert das aktuelle Paar (Schlüssel und Wert) eines Arrays und rückt den Arrayzeiger vor
- prev() - Setzt den internen Zeiger eines Arrays um ein Element zurück
- reset() - Setzt den internen Zeiger eines Arrays auf sein erstes Element
- next() - Rückt den internen Zeiger eines Arrays vor
current
18-Aug-2006 02:20
24-Apr-2004 08: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).
02-Dec-2003 11: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;
?>
08-May-2003 01: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 :)
02-Mar-2003 03: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.