Wednesday, July 29, 2026
HomeLanguagesPHP | DsSequence map() Function

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
   
// 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

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS