PHP SplObjectStorage removeAllExcept() Function
Last Updated :
23 Jun, 2023
The SplObjectStorage::removeAllExcept() function is an inbuilt function in PHP which is used to remove all objects from storage except for those contains by another storage.
Syntax:
int SplObjectStorage::removeAllExcept()
Parameters: This function accepts a single parameter $obj which specify object storage to be retain.
Return Value: This function does not return any value.
Below programs illustrate the SplObjectStorage::removeAllExcept() function in PHP:
Program 1:
<?php
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$gfg1 = new SplObjectStorage();
$gfg1 [ $obj1 ] = "Geeks" ;
$gfg2 = new SplObjectStorage();
$gfg2 [ $obj1 ] = "GFG" ;
$gfg2 [ $obj2 ] = "GeeksClasses" ;
$gfg2 [ $obj3 ] = "SUDO" ;
var_dump( count ( $gfg2 ));
$gfg2 ->removeAllExcept( $gfg1 );
var_dump( count ( $gfg2 ));
?>
|
Program 2:
<?php
$obj1 = new StdClass;
$obj2 = new StdClass;
$gfg1 = new SplObjectStorage();
$gfg1 [ $obj1 ] = "Geeks" ;
$gfg2 = new SplObjectStorage();
$gfg2 [ $obj1 ] = "GFG" ;
$gfg2 [ $obj2 ] = "GeeksClasses" ;
var_dump( count ( $gfg2 ));
$gfg2 ->removeAllExcept( $gfg1 );
var_dump( count ( $gfg2 ));
$gfg2 ->removeAllExcept( $gfg2 );
var_dump( count ( $gfg2 ));
?>
|
Output:
int(2)
int(1)
int(1)
Reference: https://www.php.net/manual/en/splobjectstorage.removeallexcept.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...