📜  PHP的ArrayObject offsetExists()函数

📅  最后修改于: 2022-05-13 01:57:40.389000             🧑  作者: Mango

PHP的ArrayObject offsetExists()函数

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

语法

bool offsetExists($index) 

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

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

下面的程序说明了上述函数:



程序一

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!

方案二

"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 /手动/ EN / arrayobject.offsetexists。 PHP