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를 반환합니다.
예제
Example #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 ¶
4 years ago
xmlich02 at stud dot fit dot vutbr dot cz ¶
5 years ago
// 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);
}
