Here's a slight revision to xmlich02's backwards iteration example. The problem with his/her example is that it will halt if any of the array elements are boolean false, while this version will not.
<?php
end($ar);
while ( !is_null($key = key($ar)) ) {
$val = current($ar);
echo "{$key} => {$val}\n";
prev($ar);
}
?>
prev
(PHP 4, PHP 5)
prev — 内部の配列ポインタをひとつ前に戻す
パラメータ
-
array -
入力の配列。
返り値
内部の配列ポインタが指している前の場所の配列値を返します。
もう要素がない場合は FALSE を返します。
例
例1 prev() および類似関数の使用例
<?php
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport); // $mode = 'bike';
$mode = next($transport); // $mode = 'car';
$mode = prev($transport); // $mode = 'bike';
$mode = end($transport); // $mode = 'plane';
?>
soapergem at gmail dot com
29-May-2009 12:06
xmlich02 at stud dot fit dot vutbr dot cz
29-Sep-2007 12:19
// example of backward iteration
$ar = array ( 'a', 'b', 'c', 'd', 'e', 'f') ;
print_r($ar);
end($ar);
while($val = current($ar)) {
echo $val.' ';
prev($ar);
}
