Open In App

PHP | Ds\Sequence map() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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
   
// 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; 
}));
   
?>


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

Program 2:




<?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; 
}));
   
?>


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
Previous
Next
Share your thoughts in the comments
Similar Reads