更新时间2019年1月7号,下面介绍一个PHP函数
pos() Function
pos()是PHP中的内建函数,用于返回当前内部指针指向的数组中元素的值。在返回值后,pos()函数不会增加或减少内部指针。在PHP中,所有数组都有一个内部指针。这个内部指针指向数组中的某个元素,该元素被称为数组的当前元素。通常,当前元素是数组中插入的第一个元素。
语法
1 | pos($array) |
参数:pos()函数接受一个参数$数组。它是我们想要找到当前元素的数组。
返回值:返回当前内部指针指向的数组中元素的值。如果数组为空,那么pos()函数返回FALSE。
下面的程序演示了PHP中的pos()函数:
程序1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php // input array $arr = array("Ram", "Shyam", "Geeta", "Rita"); // Here pos() function returns current element. echo pos($arr)."\n"; // next() function increments internal pointer // to point to next element i.e, Shyam. echo next($arr)."\n"; // Printing current position element. echo pos($arr)."\n"; // Printing previous element of the current one. echo prev($arr)."\n"; // Printing end element of the array echo end($arr)."\n"; // Printing current position element. echo pos($arr)."\n"; ?> |
输出
1 2 3 4 5 6 | Ram Shyam Shyam Ram Rita Rita |
程序2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php // input array $arr = array("P", "6", "M", "F"); // Here pos() function returns current element. echo pos($arr)."\n"; // next() function increments internal pointer // to point to next element i.e, 6. echo next($arr)."\n"; // Printing current position element. echo pos($arr)."\n"; // Printing previous element of the current one. echo prev($arr)."\n"; // Printing end element of the array echo end($arr)."\n"; // Printing current position element. echo pos($arr)."\n"; ?> |
输出
1 2 3 4 5 6 | P 6 6 P F F |
参考
http://php.net/manual/en/function.pos.php