ArrayIterator::offsetSet()函數是PHP中的內置函數,用於設置偏移量的值。
用法:
void ArrayIterator::offsetSet( mixed $index, mixed $newval )
參數:該函數接受上述和以下描述的兩個參數:
- $index:此參數保存用於設置偏移量的索引。
- $newval:此參數保存要存儲在給定索引處的新值。
返回值:該函數不返回任何值。
以下示例程序旨在說明PHP中的ArrayIterator::offsetSet()函數:
示例1:
<?php
// 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:
<?php
// 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 ) )
參考: https://www.php.net/manual/en/arrayiterator.offsetset.php
相關用法
- PHP ArrayObject offsetSet()用法及代碼示例
- PHP SplDoublyLinkedList offsetSet()用法及代碼示例
- PHP SplObjectStorage offsetSet()用法及代碼示例
- PHP ArrayIterator key()用法及代碼示例
- PHP ArrayIterator next()用法及代碼示例
- PHP ArrayIterator setFlags()用法及代碼示例
- PHP ArrayIterator rewind()用法及代碼示例
- PHP ArrayIterator getFlags()用法及代碼示例
- PHP ArrayIterator seek()用法及代碼示例
- PHP ArrayIterator seek()用法及代碼示例
- PHP ArrayIterator offsetGet()用法及代碼示例
- PHP ArrayIterator natsort()用法及代碼示例
- PHP ArrayIterator ksort()用法及代碼示例
- PHP ArrayIterator offsetUnset()用法及代碼示例
- PHP ArrayIterator getArrayCopy()用法及代碼示例
注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | ArrayIterator offsetSet() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。