当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP SplObjectStorage removeAll()用法及代码示例


SplObjectStorage::removeAll()函数是PHP中的内置函数,用于从当前存储中删除另一个存储中包含的所有对象。

用法:

void SplObjectStorage::removeAll( $obj )

参数:该函数接受单个参数$obj,该参数指定要从当前存储中删除的存储。


返回值:该函数不返回任何值。

以下示例程序旨在说明PHP中的SplObjectStorage::removeAll()函数:

程序1:

<?php 
  
$obj1 = new StdClass; 
$obj2 = new StdClass; 
  
$gfg1 = new SplObjectStorage(); 
$gfg1[$obj1] = "Geeks"; 
  
$gfg2 = new SplObjectStorage(); 
$gfg2[$obj1] = "GFG"; 
$gfg2[$obj2] = "GeeksClasses"; 
  
// Count and print all existing objects 
var_dump(count($gfg2)); 
  
// Remove all objects of $gfg1 from $gfg2 
$gfg2->removeAll($gfg1); 
  
// Print result after removeAll 
var_dump(count($gfg2)); 
?>
输出:
int(2)
int(1)

程序2:

<?php 
  
$obj1 = new StdClass; 
$obj2 = new StdClass; 
  
$gfg1 = new SplObjectStorage(); 
$gfg1[$obj1] = "Geeks"; 
  
$gfg2 = new SplObjectStorage(); 
$gfg2[$obj1] = "GFG"; 
$gfg2[$obj2] = "GeeksClasses"; 
  
// Count and print all existing objects 
var_dump(count($gfg2)); 
  
// Remove all objects of $gfg1 from $gfg2 
$gfg2->removeAll($gfg1); 
  
// Print result after removeAll 
var_dump(count($gfg2)); 
  
// Remove all objects itself $gfg2 
$gfg2->removeAll($gfg2); 
  
// Print result after removeAll 
var_dump(count($gfg2)); 
  
?>
输出:
int(2)
int(1)
int(0)

参考: https://www.php.net/manual/en/splobjectstorage.removeall.php



相关用法


注:本文由纯净天空筛选整理自R_Raj大神的英文原创作品 PHP | SplObjectStorage removeAll() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。