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


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


Ds \ Deque::apply()函數是PHP中的內置函數,用於通過執行回調函數定義的操作來更新Deque的值。

用法:

public Ds\Deque::apply( $callback ):void

參數:該函數接受單個參數$callback,該參數保存該函數定義要對Deque的每個元素執行的操作。


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

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

程序1:

<?php 
  
// Declare deque 
$deck = new \Ds\Deque([1, 2, 3, 4, 5, 6]); 
  
echo("\nElements in the deque are\n"); 
  
// Display the deque elements 
print_r($deck); 
  
// Use apply() function to implement 
// the operation 
$deck->apply(function($element) {  
    return $element * 10;  
}); 
  
echo("\nUpdated elements in the deque\n"); 
  
// Display the deque elements 
print_r($deck); 
  
?> 
輸出:
Elements in the deque are
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Updated elements in the deque
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

程序2:

<?php 
  
// Declare deque 
$deck = new \Ds\Deque([10, 20, 30, 40, 50, 60]); 
  
echo("\nElements in the deque are\n"); 
  
// Display the deque elements 
print_r($deck); 
  
// Use apply() function to implement 
// the operation 
$deck->apply(function($element) {  
    return $element % 10;  
}); 
  
echo("\nUpdated elements in the deque\n"); 
  
// Display the deque elements 
print_r($deck); 
  
?> 
輸出:
Elements in the deque are
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

Updated elements in the deque
Ds\Deque Object
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
)

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



相關用法


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