PHP中ArrayObject类的offsetSet()函数用于更新ArrayObject中特定索引处的值。
用法:
void offsetSet($index, $val)
参数:此函数接受两个参数$index和$val。此函数使用$val更新索引$index上的值。
Return Value:此函数不返回任何值。
以下示例程序旨在说明上述功能:
程序1::
// PHP program to illustrate the
// offsetSet() function
$arr = array("geeks100", "geeks99", "geeks1", "geeks02");
// Create array object
$arrObject = new ArrayObject($arr);
// Update the value at index 1
$arrObject->offsetSet(1, "Updated");
// Print the updated ArrayObject
print_r($arrObject);
?>
输出:
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[0] => geeks100
[1] => Updated
[2] => geeks1
[3] => geeks02
)
)
程序2::
// PHP program to illustrate the
// offsetSet() function
$arr = array("Welcome"=>"1", "to" => "2", "GfG" => "3");
// Create array object
$arrObject = new ArrayObject($arr);
// Update the value at index "to"
$arrObject->offsetSet("to", "UpdatedValue");
// Print the updated ArrayObject
print_r($arrObject);
?>
输出:
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[Welcome] => 1
[to] => UpdatedValue
[GfG] => 3
)
)