Friday, October 10, 2025
HomeLanguagesArrayObject offsetSet() Function in PHP

ArrayObject offsetSet() Function in PHP

The offsetSet() function of the ArrayObject class in PHP is used to update the value present at a specific index in the ArrayObject.

Syntax:

void offsetSet($index, $val) 

Parameters: This function accepts two parameters $index and $val. This function updates the value present at the index, $index with $val.

Return Value: This function does not returns any value.

Below programs illustrate the above function:

Program 1:




<?php
// PHP program to illustrate the
// offsetSet() function
  
$arr = array("neveropen100", "neveropen99", "neveropen1", "neveropen02");
  
// Create array object
$arrObject = new ArrayObject($arr);
  
// Update the value at index 1
$arrObject->offsetSet(1, "Updated");
  
// Print the updated ArrayObject
print_r($arrObject);
  
?>


Output:

ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => neveropen100
            [1] => Updated
            [2] => neveropen1
            [3] => neveropen02
        )

)

Program 2:




<?php
// 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);
  
?>


Output:

ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [Welcome] => 1
            [to] => UpdatedValue
            [GfG] => 3
        )

)

Reference: http://php.net/manual/en/arrayobject.offsetset.php

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7100 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS