Sunday, June 14, 2026
HomeLanguagesPHP | DsVector map() Function

PHP | Ds\Vector map() Function

The Ds\Vector::map() function is an inbuilt function in PHP which is used to return the result of a callback after applying to each value in the vector.

Syntax:

Ds\Vector public Ds\Vector::map( $callback )

Parameters: This function accepts single parameter $callback which is to be applied to each vector elements.

Return Value: This function returns the vector after applying the callback to each value in the vector.

Below programs illustrate the Ds\Vector::map() function in PHP:

Program 1:




<?php
  
// Create new Vector
$vector = new \Ds\Vector([1, 2, 3, 4, 5]);
  
// Display the Vector element after
// applying the callback function
print_r($vector->map(function($value) { 
    return $value * 10; 
}));
  
?>


Output:

Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Program 2:This program shows the implementation of map() function which sets 1 in the vector for each element satisfying the condition in callback.




<?php
  
// Create new Vector
$vector = new \Ds\Vector([10, 20, 30, 40, 50]);
  
// Display the Vector element after
// applying the callback function
print_r($vector->map(function($value) { 
    return $value <= 30;
}));
  
?>


Output:

Ds\Vector Object
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 
    [4] => 
)

Reference: http://php.net/manual/en/ds-vector.map.php

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 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