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


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


Ds \ Deque::pop()函數是PHP中的內置函數,用於從Deque中刪除最後一個元素(如果Deque不為空)並返回它。如果Deque為空,則拋出異常。

用法:

public Ds\Deque::pop( void ):mixed

參數:該函數不接受任何參數。


返回值:此函數返回從雙端隊列中刪除的最後一個元素。

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

程序1:

<?php 
  
// Declare a deque 
$deck = new \Ds\Deque([10, 20, 30, 40, 50, 60]); 
  
echo("Elements of Deque\n"); 
  
// Display the Deque elements 
print_r($deck); 
  
// last element in the deque 
echo("\nLast element of deque:"); 
  
// Use pop() function to removing last 
// element from Deque and Display it 
print_r($deck->pop()); 
  
echo("\nElements of Deque\n"); 
  
// Display the Deque elements 
print_r($deck); 
  
?>
輸出:
Elements of Deque
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

Last element of deque:60
Elements of Deque
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

程序2:

<?php 
  
// Declare a deque 
$deck = new \Ds\Deque(["geeks", "for", "geeks", "practice"]); 
  
echo("Elements of Deque\n"); 
  
// Display the Deque elements 
print_r($deck); 
  
// last element in the deque 
echo("\nLast element of deque:"); 
  
// Use pop() function to removing last 
// element from Deque and Display it 
print_r($deck->pop()); 
  
echo("\nElements of Deque\n"); 
  
// Display the Deque elements 
print_r($deck); 
  
?>
輸出:
Elements of Deque
Ds\Deque Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
    [3] => practice
)

Last element of deque:practice
Elements of Deque
Ds\Deque Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
)

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



相關用法


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