Ds \ Deque::map()函數是PHP中的內置函數,用於返回Deque,並根據根據回調函數執行的操作修改每個元素。
用法:
public Ds\Deque::map( $callback ):Ds\Deque
參數:該函數接受單個參數$callback,該參數包含要在Deque的每個元素上執行的操作上的可調用函數。
返回值:此函數返回一個修改了每個元素的雙端隊列。
以下示例程序旨在說明PHP中的Ds \ Deque::map()函數:
程序1:
<?php
// Declare a Deque
$deck = new \Ds\Deque([1, 2, 3, 4, 5, 6]);
echo("Elements of deque\n");
// Display the Elements of Deque
print_r($deck);
// Deque after mapping each value as
// per in the callable function
print_r($deck->map(function($element) {
// performing operation on each element
return $element * 10;
}));
?>
輸出:
Elements of deque Ds\Deque Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) Ds\Deque Object ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 [5] => 60 )
程序2:
<?php
// Declare a Deque
$deck = new \Ds\Deque([10, 20, 30, 40, 50, 60]);
echo("Elements of deque\n");
// Display the Elements of Deque
print_r($deck);
// Deque after mapping each value as
// per in the callable function
print_r($deck->map(function($element) {
// performing operation on each element
return $element / 10;
}));
?>
輸出:
Elements of deque Ds\Deque Object ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 [5] => 60 ) Ds\Deque Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
參考: http://php.net/manual/en/ds-deque.map.php
相關用法
- d3.js d3.lab()用法及代碼示例
- PHP exp()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- d3.js d3.hcl()用法及代碼示例
- PHP sin( )用法及代碼示例
- PHP abs()用法及代碼示例
- PHP cos( )用法及代碼示例
- PHP tan( )用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP next()用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- d3.js d3.sum()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 PHP | Ds\Deque map() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。