next()与prev()
PHP prev()函数 (PHP prev() function)
prev() function firstly moves the current pointer to the previous element and returns the element.
prev()函数首先将当前指针移动到上一个元素,然后返回该元素。
Syntax:
句法:
prev(array);
Note: As we have discussed in last two posts (current() function in PHP and next() function in PHP) that an array has a pointer that points to the first element by default, next() function can be used to move the pointer to next element and then to print it and similarly a prev() function can be used to move the pointer to previous element and print it.
注意:正如我们在前两篇文章( PHP中的current()函数和PHP中的 next()函数 )中所讨论的那样,数组默认具有指向第一个元素的指针, next()函数可用于移动数组。指向下一个元素的指针然后进行打印,类似地,可以使用prev()函数将指针移至上一个元素并进行打印。
Examples:
例子:
Input:
$arr1 = array(10, 20, 30, 40, 50);
Function call:
current($arr1);
next($arr1);
prev($arr1);
Output:
10
20
10
Input:
$arr2 = array("name" => "Amit", "age" => 21, "Gender" => "Male");
Function call:
current($arr2);
next($arr2);
prev($arr2);
Output:
Amit
21
Amit
PHP code:
PHP代码:
<?php
$arr1 = array(10, 20, 30, 40, 50);
$arr2 = array("name" => "Amit", "age" => 21, "Gender" => "Male");
echo current($arr1) . "\n"; //print first element
echo next($arr1) . "\n"; //move to next element and print
echo prev($arr1) . "\n"; //move to previous element and print
echo current($arr2) . "\n"; //print first element
echo next($arr2) . "\n"; //move to next element and print
echo prev($arr2) . "\n"; //move to previous element and print
?>
Output
输出量
10
20
10
Amit
21
Amit
翻译自: https://www.includehelp.com/php/prev-function-with-example.aspx
next()与prev()