PHP | Ds\Vector apply() Function

The Ds\Vector::apply() function is an inbuilt function in PHP which is used to update all values in the array by applying the callback function to each value of the vector. After this callback, all the values of the vector will get modified as defined in the callback function.

Syntax:

public Ds\Vector::apply( $callback ) : void

Parameters: This function accepts single parameter $callback which is used to update the values in the vector. This callback function should return the value by which vector element is to be replaced.

Return Value: This function does not return any value.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare the callback function
$callback = function($value) {
    return $value / 10; 
};
  
// Declare a vector
$vector = new \Ds\Vector([10, 20, 30, 40, 50]);
  
// Use apply() function to call function
$vector->apply($callback);
  
// Display the vector element
print_r($vector);
?> 

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare the callback function
$callback = function($value) {
    return $value * 5;
};
  
// Declare a vector
$vector = new \Ds\Vector([1, 2, 3, 4, 5]);
  
// Use apply() function to call function
$vector->apply($callback);
  
// Display the vector element
print_r($vector);
?> 

chevron_right


Output:

Ds\Vector Object
(
    [0] => 5
    [1] => 10
    [2] => 15
    [3] => 20
    [4] => 25
)

Reference: http://php.net/manual/en/ds-vector.apply.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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.