PHP | Ds\Deque map() Function

The Ds\Deque::map() function is an inbuilt function in PHP which is used to return the Deque with each element modified on the basis of operation performed as per the callback function.
Syntax:
public Ds\Deque::map( $callback ) : Ds\Deque
Parameters: This function accepts single parameter $callback which contains the callable function on the operation to be performed on each element of the Deque.
Return Value: This function returns a Deque with each element modified.
Below programs illustrate the Ds\Deque::map() function in PHP:
Program 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; })); ?> |
Output:
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
)
Program 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; })); ?> |
Output:
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
)
Reference: http://php.net/manual/en/ds-deque.map.php
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



