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
相关用法
- PHP SplFixedArray offsetExists()用法及代码示例
- PHP SplObjectStorage offsetExists()用法及代码示例
- PHP SplDoublyLinkedList offsetExists()用法及代码示例
- PHP ArrayIterator offsetExists()用法及代码示例
- PHP ArrayObject setFlags()用法及代码示例
- PHP ArrayObject natcasesort()用法及代码示例
- PHP ArrayObject setIteratorClass()用法及代码示例
- PHP ArrayObject unserialize()用法及代码示例
- PHP ArrayObject serialize()用法及代码示例
- PHP ArrayObject getFlags()用法及代码示例
- PHP ArrayObject uksort()用法及代码示例
- PHP ArrayObject append()用法及代码示例
- PHP ArrayObject getIteratorClass()用法及代码示例
- PHP ArrayObject offsetGet()用法及代码示例
- PHP ArrayObject offsetUnset()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 ArrayObject offsetExists() Function in PHP。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。