ArrayIterator::offsetSet()函数是PHP中的内置函数,用于设置偏移量的值。
用法:
void ArrayIterator::offsetSet( mixed $index, mixed $newval )
参数:该函数接受上述和以下描述的两个参数:
$index:此参数保存用于设置偏移量的索引。
$newval:此参数保存要存储在给定索引处的新值。
返回值:该函数不返回任何值。
以下示例程序旨在说明PHP中的ArrayIterator::offsetSet()函数:
示例1:
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
array(
"a" => 4,
"b" => 2,
"g" => 8,
"d" => 6,
"e" => 1,
"f" => 9
)
);
// Update the value at index 1
$arrItr->offsetSet("g", "Geeks");
// Print the updated ArrayObject
print_r($arrItr);
?>
输出:
ArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[a] => 4
[b] => 2
[g] => Geeks
[d] => 6
[e] => 1
[f] => 9
)
)
示例2:
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
array(
"for", "Geeks", "Science",
"Geeks", "Portal", "Computer"
)
);
// Update the value at index 1
$arrItr->offsetSet(1, "GeeksforGeeks");
// Print the updated ArrayObject
print_r($arrItr);
?>
输出:
ArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[0] => for
[1] => GeeksforGeeks
[2] => Science
[3] => Geeks
[4] => Portal
[5] => Computer
)
)