PHP Conference Japan 2024

ArrayObject::offsetExists

(PHP 5, PHP 7, PHP 8)

ArrayObject::offsetExists回傳要求的索引是否存在

描述

public ArrayObject::offsetExists(mixed $key): bool

參數

key

要檢查的索引。

回傳值

如果要求的索引存在,則回傳 true,否則回傳 false

範例

範例 #1 ArrayObject::offsetExists() 範例

<?php
$arrayobj
= new ArrayObject(array('zero', 'one', 'example'=>'e.g.'));
var_dump($arrayobj->offsetExists(1));
var_dump($arrayobj->offsetExists('example'));
var_dump($arrayobj->offsetExists('notfound'));
?>

以上範例將會輸出

bool(true)
bool(true)
bool(false)

新增註解

使用者貢獻的註解 1 則註解

goran at extensionsforjoomla dot com
17 年前
在 PHP 5.2.2 之前的版本中,如果索引值為 null,offsetExists() 會回傳 false。
<?php
// 執行 PHP 5.2.1
$params = new ArrayObject(array('INT'=>null, 'STR'=> null, 'BOOL'=>null, 'LOB'=>null));
$test = $params->offsetExists('INT');
var_dump($test);
// 結果會是 bool(false)
// 執行 PHP 5.2.2
$params = new ArrayObject(array('INT'=>null, 'STR'=> null, 'BOOL'=>null, 'LOB'=>null));
$test = $params->offsetExists('INT');
var_dump($test);
// 結果會是 bool(true)
?>
兩個測試都是在 Windows 平台上執行。
To Top