PHP | Ds\Sequence map() Function
The Ds\Sequence::map() function is an inbuilt function in PHP which returns the result after applying callback function to each value.
Syntax:
Ds\Sequence abstract public Ds\Sequence::map( $callback )
Parameter: This function accepts single parameter $callback. The callback apply on each value of sequence.
Return Value: This function returns the callback after applying on each value.
Below programs illustrate the Ds\Sequence::map() function in PHP:
Program 1:
<?php
$seq = new \Ds\Vector( [4, 8, 12, 16] );
var_dump( $seq ->map( function ( $val ) {
return $val * 3;
}));
?>
|
Output:
object(Ds\Vector)#3 (4) {
[0]=>
int(12)
[1]=>
int(24)
[2]=>
int(36)
[3]=>
int(48)
}
Program 2:
<?php
$seq = new \Ds\Vector([12, 15, 18, 20]);
var_dump( $seq ->map( function ( $val ) {
return $val / 3;
}));
?>
|
Output:
object(Ds\Vector)#3 (4) {
[0]=>
int(4)
[1]=>
int(5)
[2]=>
int(6)
[3]=>
float(6.6666666666667)
}
Reference: http://php.net/manual/en/ds-sequence.map.php
Last Updated :
21 Aug, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...