当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP ArrayObject offsetExists()用法及代码示例


PHP中ArrayObject类的offsetExists()函数用于确定ArrayObject中是否存在给定的偏移量或索引。如果存在,则该函数返回布尔True值,否则返回False。

用法

bool offsetExists($index) 

参数:此函数接受单个参数$index,这是要检查是否存在于ArrayObject中的索引。


Return Value:此函数根据索引是否存在于ArrayObject中,返回布尔值True或False。

以下示例程序旨在说明上述函数:

程序1:

<?php 
// PHP program to illustrate the 
// offsetExists() function 
  
$arr = array("geeks100", "geeks99", "geeks1", "geeks02"); 
  
// Create array object 
$arrObject = new ArrayObject($arr); 
  
// Print the ArrayObject 
print_r($arrObject); 
  
// Check if the Key 1 is present  
if($arrObject->offsetExists(1)) 
    echo "\nThe key 1 is present!"; 
else
    echo "\nThe key 1 is not present!"; 
  
// Check if the Key 20 is present  
if($arrObject->offsetExists(20)) 
    echo "\nThe key 20 is present!"; 
else
    echo "\nThe key 20 is not present!";     
      
?>
输出:
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => geeks100
            [1] => geeks99
            [2] => geeks1
            [3] => geeks02
        )

)

The key 1 is present!
The key 20 is not present!

程序2:

<?php 
// PHP program to illustrate the 
// offsetExists() function 
  
$arr = array("Welcome"=>"1", "to" => "2", "GfG" => "3"); 
  
// Create array object 
$arrObject = new ArrayObject($arr); 
  
// Print the ArrayObject 
print_r($arrObject); 
  
// Check if the Key "Welcome" is present  
if($arrObject->offsetExists("Welcome")) 
    echo "\nThe key Welcome is present!"; 
else
    echo "\nThe key Welcome is not present!"; 
  
// Check if the Key GfG is present  
if($arrObject->offsetExists("GfG")) 
    echo "\nThe key GfG is present!"; 
else
    echo "\nThe key GfG is not present!";     
      
?>
输出:
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [Welcome] => 1
            [to] => 2
            [GfG] => 3
        )

)

The key Welcome is present!
The key GfG is present!

参考: http://php.net/manual/en/arrayobject.offsetexists.php



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 ArrayObject offsetExists() Function in PHP。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。