當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。