Wednesday, June 17, 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

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS