PHP | Ds\Deque apply()函数
Ds\Deque::apply()函数是PHP中的一个内置函数,用于通过执行回调函数定义的操作来更新 Deque 的值。
句法:
public Ds\Deque::apply( $callback ) : void
参数:此函数接受单个参数$callback ,其中包含定义要对 Deque 的每个元素执行的操作的函数。
返回值:该函数不返回任何值。
下面的程序说明了PHP中的Ds\Deque::apply()函数:
方案一:
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
)
方案二:
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。 PHP