ArrayObject::offsetExists()
函式用於檢查給定的偏移量(即陣列下標)在 ArrayObject
物件中是否存在。
用法:
bool ArrayObject::offsetExists( mixed $offset )
引數:
$offset
:要檢查的偏移量(陣列下標)。
返回值: 如果偏移量存在,則返回 true
,否則返回 false
。
示例:
// 建立 ArrayObject 物件
$fruits = new ArrayObject(['apple', 'banana', 'orange']);
// 檢查偏移量是否存在並輸出結果
if ($fruits->offsetExists(1)) {
echo "偏移量存在";
} else {
echo "偏移量不存在";
}
輸出結果:
偏移量存在
在上面的例子中,我們建立了一個包含三個元素的 ArrayObject
物件 $fruits
,然後使用 offsetExists()
函式檢查偏移量 1
是否存在。由於偏移量 1
對應的元素存在於陣列中,因此函式返回 true
,最終輸出結果為 "偏移量存在"。