Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Vector::apply() Function



The PHP Ds\Vector::apply() function is used to update all values in a vector by applying a callback function to each value.

The callback function returns the updated value by performing operations on each element such as addition, subtraction, multiplication, etc., and that updated value is stored back in the vector.

Syntax

Following is the syntax of the PHP Ds\Vector::apply() function −

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

Parameters

Following is the parameter of this function −

  • callback − A callable function applies to each value in a vector.

Following is the syntax of the callback function −

callback(mixed $value): mixed

Return value

This function doesn't return any value.

Example 1

The following is the basic example of the PHP Ds\Vector::apply() function −

<?php 
   $vector = new \Ds\Vector([10, 20, 30, 40, 50]); 
   echo "The vector elements are: \n";
   print_r($vector);
   #callback function   
   $callback = function($value) { 
      return $value / 5;  
   };
   #using apply() function
   $vector->apply($callback);
   echo "The updated vector elements are: \n"; 
   print_r($vector); 
?>

Output

After executing the above program, the following output will be displayed −

The vector elements are:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The updated vector elements are:
Ds\Vector Object
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
)

Example 2

Following is another example of the PHP Ds\Vector::apply() function. We use this function to update all values by applying the callback function on each element of this vector (["Tutorials", "Point", "India"]) −

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "India"]); 
   echo "The vector elements are: \n";
   print_r($vector);
   #callback function   
   $callback = function($value) { 
      return strtoupper($value);  
   };
   #using apply() function
   $vector->apply($callback);
   echo "The updated vector elements are: \n"; 
   print_r($vector); 
?>

Output

The above program generates the following output −

The vector elements are:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
)
The updated vector elements are:
Ds\Vector Object
(
    [0] => TUTORIALS
    [1] => POINT
)

Example 3

In the example below, we use the apply() function to update all the vector elements by applying the callback function. The callback function multiplies each element with 2 and adds 10

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]); 
   echo "The vector elements are: \n";
   print_r($vector);
   #callback function   
   $callback = function($value) { 
      return $value*2 + 10;  
   };
   #using apply() function
   $vector->apply($callback);
   echo "The updated vector elements are: \n"; 
   print_r($vector);
?>

Output

Once the above program is executed, it generates the following output −

The vector elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The updated vector elements are:
Ds\Vector Object
(
    [0] => 12
    [1] => 14
    [2] => 16
    [3] => 18
    [4] => 20
)
php_function_reference.htm
Advertisements