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