PHP - Ds Map::clear() 函数



PHP 的 Ds\Map::clear() 函数用于移除当前映射中的所有值。如果我们调用此函数的映射已经为空,则不会影响映射,并且它将保持不变。

调用 clear() 函数后,当前映射将为空。您可以使用 isEmpty() 函数验证这一点,如果映射为空,则该函数返回“true”,否则返回“false”。

语法

以下是 PHP Ds\Map::clear() 函数的语法:

public Ds\Map::clear(): void

参数

此函数不接受任何参数。

返回值

此函数不返回值。

示例 1

以下是 PHP Ds\Map::clear() 函数的基本示例:

<?php 
   $map = new \Ds\Map([1, 2, 3, 4, 5]);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
   echo "[".$key."]"." = ".$value."\n";
   }
   #using clear() function
   $map->clear(); 
   echo "The map after the clear() function called: \n";
   print_r($map); 
?>

输出

以上程序输出如下:

The map elements are:
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
The map after the clear() function called:
Ds\Map Object
(
)

示例 2

以下是 PHP Ds\Map::clear() 函数的另一个示例。我们使用此函数清除此映射的所有值(["Tutorials", "Point", "India"]):

<?php 
   $map = new \Ds\Map(["Tutorials", "Point", "India"]);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
   echo "[".$key."]"." = ".$value."\n";
   }
   #using clear() function
   $map->clear(); 
   echo "The map after the clear() function called: \n";
   print_r($map); 
?>

输出

执行上述程序后,将显示以下输出:

The map elements are:
[0] = Tutorials
[1] = Point
[2] = India
The map after the clear() function called:
Ds\Map Object
(
)

示例 3

如果我们调用的当前映射已经为空,则 clear() 函数不会影响映射,并且映射将保持不变:

<?php 
   $map = new \Ds\Map([]);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
   echo "[".$key."]"." = ".$value."\n";
   }
   #using clear() function
   $map->clear(); 
   echo "The map after the clear() function called: \n";
   print_r($map); 
?>

输出

执行上述程序后,将生成以下输出:

The map elements are:
The map after the clear() function called:
Ds\Map Object
(
)
php_function_reference.htm
广告

© . All rights reserved.