當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP Ds\Deque set()用法及代碼示例


Ds \ Deque::set()函數是PHP中的內置函數,用於在Deque中給定索引處設置值。

用法:

public Ds\Deque::set( $index, $value ):void

參數:該函數接受上述和以下所述的兩個參數:


  • index:此參數保存要在其上設置元素的索引值。
  • value:此參數保存要在給定索引處設置的值。

返回值:該函數不返回任何值。

以下示例程序旨在說明PHP中的Ds \ Deque::set()函數:

程序1:

<?php 
  
// Declare a deque 
$deck = new \Ds\Deque(["geeks", "for", "geeks", "practice"]); 
  
echo("Elements of Deque\n"); 
  
// Display the Deque elements 
print_r($deck); 
  
// Updating the deque element at index 2 
$deck->set(2, "geeksforgeeks"); 
   
echo("Deque after setting value at index 2\n"); 
  
// Display the Deque elements 
print_r($deck); 
  
?>
輸出:
Elements of Deque
Ds\Deque Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
    [3] => practice
)
Deque after setting value at index 2
Ds\Deque Object
(
    [0] => geeks
    [1] => for
    [2] => geeksforgeeks
    [3] => practice
)

程序2:

<?php 
  
// Declare a deque 
$deck = new \Ds\Deque([1, 2, 3, 4, 5, 6]); 
  
echo("Elements of Deque\n"); 
  
// Display the Deque elements 
print_r($deck); 
  
// Updating the deque element at index 2 
$deck->set(4, 10); 
   
echo("Deque after setting value at index 2\n"); 
  
// Display the Deque elements 
print_r($deck); 
  
?>
輸出:
Elements of Deque
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
Deque after setting value at index 2
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 10
    [5] => 6
)

參考: http://php.net/manual/en/ds-deque.set.php



相關用法


注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 PHP | Ds\Deque set() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。