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

ArrayObject offsetUnset() Function in PHP

The offsetUnset() function of the ArrayObject class in PHP is used to unset the value preset at a specific index. In other words, it is used to remove a value present at a specific index in the ArrayObject.

Syntax:

void offsetUnset($index) 

Parameters: This function accepts a single parameter $index which is the index whose value is to be unset.

Return Value: This function does not returns any value.

Below programs illustrate the above function:

Program 1:




<?php
// PHP program to illustrate the
// offsetUnset() function
  
$arr = array("Welcome"=>"1", "to" => "2", "GfG" => "3");
  
// Create array object
$arrObject = new ArrayObject($arr);
  
// Unset the value at index "to"
$arrObject->offsetUnset("to");
  
// Print the updated ArrayObject
print_r($arrObject);
  
?>


Output:

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

)

Program 2:




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


Output:

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

)

Reference: http://php.net/manual/en/arrayobject.offsetunset.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