php8.5
Home/ Manual/ ds / vector/ Ds\Vector::set

Ds\Vector::set

PHP function Edit on GitHub ✎

(PECL ds >= 1.0.0)

Updates a value at a given index

Description

Ds\Vector::set(int $index, mixed $value): void

Updates a value at a given index.

Parameters

index

The index of the value to update.

value

The new value.

Return Values

Void

Errors/Exceptions

OutOfRangeException if the index is not valid.

Examples

Ds\Vector::set() example

php
<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

$vector->set(1, "_");
print_r($vector);
?>

The above example will output something similar to:

output
Ds\Vector Object
(
    [0] => a
    [1] => _
    [2] => c
)

Ds\Vector::set() example using array syntax

php
<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

$vector[1] = "_";
print_r($vector);
?>

The above example will output something similar to:

output
Ds\Vector Object
(
    [0] => a
    [1] => _
    [2] => c
)

Source: reference/ds/ds/vector/set.xml · from the official PHP manual (php/doc-en)