PHP | pos()函数用法详细指南

2021年3月13日15:38:41 发表评论 714 次浏览

pos()是PHP中的内置函数, 用于返回内部指针当前指向的数组中元素的值。返回值后, pos()函数不会递增或递减内部指针。在PHP中, 所有数组都有一个内部指针。此内部指针指向该数组中的某个元素, 称为该数组的当前元素。通常, 当前元素是数组中第一个插入的元素。

语法如下:

pos($array)

参数:pos()函数接受单个参数$数组。这是我们要查找当前元素的数组。

返回值:它返回内部指针当前指向的数组中元素的值。如果数组为空, 则pos()函数将返回FALSE。

下面的程序说明了PHP中的pos()函数:

程序1:

<?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" ; 
?>

输出如下:

Ram
Shyam
Shyam
Ram
Rita
Rita

程序2:

<?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" ; 
?>

输出如下:

P
6
6
P
F
F

参考:

http://php.net/manual/en/function.pos.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: