Open In App

PHP | Ds\Map apply() Function

Last Updated : 20 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The Ds\Map::apply() function of the Map class in PHP is used to apply a specific operation to all of the elements present in the map. It accepts a callback function and updates all of the elements present in the Map according to the given callback function.

Syntax:

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

Note: The callback function should return the updated value for specific key-value pair.

Parameters: It accepts a callback function as a parameter and updates all of the elements according to that function.

Return value: This function does not returns any value.

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

Program 1:




<?php
// PHP program to illustrate the apply()
// function of Ds\map
  
// Creating a Map
$map = new \Ds\Map(["1" => "Geeks"
            "2" => "for", "3" => "Geeks"]);
  
// Converting all elements to uppercase 
// using callback function
$map->apply(function($key, $value){ 
                return strtoupper($value);
            });
  
print_r($map);
  
?>


Output:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => GEEKS
        )

    [1] => Ds\Pair Object
        (
            [key] => 2
            [value] => FOR
        )

    [2] => Ds\Pair Object
        (
            [key] => 3
            [value] => GEEKS
        )

)

Program 2:




<?php
// PHP program to illustrate the apply()
// function of Ds\map
  
// Creating a Map
$map = new \Ds\Map(["1" => 5, 
            "2" => 10, "3" => 15]);
  
// Declare the callback function
$callback = function($key, $value){
                return $value*10;
            };
              
// Multiplying each value by 10
// using callback function
$map->apply($callback);
  
print_r($map);
  
?>


Output:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => 50
        )

    [1] => Ds\Pair Object
        (
            [key] => 2
            [value] => 100
        )

    [2] => Ds\Pair Object
        (
            [key] => 3
            [value] => 150
        )

)


Similar Reads

PHP | Ds\Map map() Function
The Ds\Map::map() function of the Map class in PHP is used to apply a callback function to a Map object. This returns the result of applying the callback function to each value present on the map. The function does not update the values in the original map, instead, it just returns the result of the updates without affecting the original values. Sy
2 min read
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: void public Ds\Vector::apply( $callback ) Parameters: This funct
2 min read
PHP | Ds\Deque apply() Function
The Ds\Deque::apply() function is an inbuilt function in PHP which is used to update the values of Deque by performing operations as defined by the callback function. Syntax: public Ds\Deque::apply( $callback ) : void Parameters: This function accepts single parameter $callback which holds the function define the operation to be performed on each e
2 min read
PHP | Ds\Sequence apply() Function
The Ds\Sequence::apply() function is an inbuilt function in PHP which is used to updates all value of sequence by applying a callback function to each value. Syntax: void abstract public Ds\Sequence::apply ( callable $callback ) Parameter: This function accepts single parameter $callback which is used to apply to each value in the sequence. Return
1 min read
How to map array values without using map method in JavaScript ?
Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each array element. This can also be implemented using for loop in JavaScript. Approach: For this, we can create two arrays, in which one array contains the array elements that are to be m
2 min read
What is image map &amp; how to map the image in HTML ?
In this article, we will discuss an image map in HTML, along with understanding its implementation through the examples. An Image Map refers to the clickable image having a clickable area that can be used to navigate to the various link to other web pages or the specific section of the same web page. The &lt;map&gt; tag can be used to define the im
2 min read
Create a Map with Google Map Api using React-Native
In this project, we'll explore how to integrate Google Maps into a React Native application. We'll create a dynamic map that allows users to view their current location and interact with markers on the map. This project aims to provide a practical guide for developers looking to incorporate maps into their mobile applications using React Native. Ou
3 min read
Difference between Function.prototype.apply and Function.prototype.call
JavaScript treats everything as an object, even functions, and every object has its own properties and methods. Function objects have both apply() and call() methods on them. However, there is confusion about the two functions in JavaScript. The main difference between them is how they handle function arguments. There is no difference between these
2 min read
PHP | Ds\Deque map() Function
The Ds\Deque::map() function is an inbuilt function in PHP which is used to return the Deque with each element modified on the basis of operation performed as per the callback function. Syntax: public Ds\Deque::map( $callback ) : Ds\Deque Parameters: This function accepts single parameter $callback which contains the callable function on the operat
2 min read
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
2 min read