Can a method be used as an array_map function

Related searches

I want to do something like this:

class Cls {
  function fun($php) {
    return 'The rain in Spain.';
  }
}

$ar = array(1,2,3);
$instance = new Cls();
print_r(array_map('$instance->fun', $ar));
               // ^ this won't work

but the first argument to array_map is supposed to be the name of the function. I want to avoid writing a wrapper function around $instance->fun, but it doesn't seem like that's possible. Is that true?

Yes, you can have callbacks to methods, like this:

array_map(array($instance, 'fun'), $ar)

see the callback type in PHP's manual for more info

Can a method be used as an array_map function, An interesting use of this function is to construct an array of arrays, which can be you can take advantage of the fact that 1) the callback method on array_map� The map () method creates a new array with the results of calling a function for every array element. The map () method calls the provided function once for each element in an array, in order. Note: map () does not execute the function for array elements without values. Note: this method does not change the original array.

You can also use

array_map('Class::method', $array) 

syntax.

array_map - Manual, So here i would like to share simple example on using class method as a callback to the array_map() function. Syntax : //Regular functions:� The below example shows how to use the map() method (along with the call() method borrowing function) on String to return a array of uppercased letters. var map = Array.prototype.map; var a = map.call('hello', function(x) { return x.toUpperCase(); }); console.log(a);

Actually, you need to know the definition of Callback, please kindly refer to the following code:

<?php 

// An example callback function
function my_callback_function() {
    echo 'hello world!';
}

// An example callback method
class MyClass {
    static function myCallbackMethod() {
        echo 'Hello World!';
    }
}

$myArray = [1, 2, 3, 4];

// Type 1: Simple callback
array_map('my_callback_function', $myArray); 

// Type 2: Static class method call
array_map(array('MyClass', 'myCallbackMethod'), $myArray); 

// Type 3: Object method call
$obj = new MyClass();
array_map(array($obj, 'myCallbackMethod'), $myArray);

// Type 4: Static class method call (As of PHP 5.2.3)
array_map('MyClass::myCallbackMethod', $myArray);

// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
    public static function who() {
        echo "A\n";
    }
}

class B extends A {
    public static function who() {
        echo "B\n";
    }
}

array_map(array('B', 'parent::who'), $myArray); // A
?>

From: http://php.net/manual/en/language.types.callable.php

How to use array_map() with class method?, Creating an array of arrays using array_map(): We can also use the array_map() function in PHP to create array of arrays. To do this we have to� The map function is used to show a list of elements from an array. Properly saying, The map () method creates a new array with the results of calling a provided function on every element in the calling array. Map method is commonly used in JavaScript, as you can see in the example given below:

PHP, Tip: You can assign one array to the function, or as many as you like. Syntax. array_map(myfunction, array1, array2, array3, ) Parameter Values� Returns : Returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.) NOTE : The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) .

PHP array_map() Function, Simplify the way you write your JavaScript by using .map(), .reduce() and .filter() instead of for() and forEach() loops. You’ll end up with clearer, less clunky code!

For example, by using the map() function, you can convert a list of String into a List of Integer by applying the Integer.valueOf() method to each String on the input list.

Comments
  • Neat. Didn't know this was doable. +1
  • If the method is static array('Class', 'StaticMethod') works too
  • This is sooo useful (And not documented on php.net's array_map page. Thanks :)
  • what if its an abstract class? which has the function to be called.
  • @amitchhajer if it's a static function, you can use array('Class_Name', 'functionName') as the parameter. If it's the current class' parent class, then using array($this, 'functionName') should most likely work.
  • This also works for the variation array_map('self::method', $array)