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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create new sequence
$seq new \Ds\Vector( [4, 8, 12, 16] );
  
// Use map() function and display it
var_dump($seq->map(function($val) { 
        return $val * 3; 
}));
   
?>

chevron_right


Output:
object(Ds\Vector)#3 (4) {
  [0]=>
  int(12)
  [1]=>
  int(24)
  [2]=>
  int(36)
  [3]=>
  int(48)
}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create new sequence
$seq new \Ds\Vector([12, 15, 18, 20]);
  
// Use map() function and display it 
var_dump($seq->map(function($val) { 
        return $val / 3; 
}));
   
?>

chevron_right


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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.