32
    class theClass{
         function doSomeWork($var){
            return ($var + 2);
         }

         public $func = "doSomeWork";

         function theFunc($min, $max){
            return (array_map(WHAT_TO_WRITE_HERE, range($min, $max)));
         }
    }

$theClass = new theClass;
print_r(call_user_func_array(array($theClass, "theFunc"), array(1, 5)));
exit;

Can any one tell what i can write at WHAT_TO_WRITE_HERE, so that doSomeWork function get pass as first parameter to array_map. and code work properly.

And give out put as

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 7
)
hakre
  • 193,403
  • 52
  • 435
  • 836
Poonam Bhatt
  • 10,154
  • 16
  • 53
  • 72
  • It should work if you just wrote the function name there, "doSomeWork" – Amjad Masad Dec 28 '10 at 14:12
  • 1
    `array($this, $this->func)` will work, when passing an object you need to pass in an array where the first arg is the object and the second is the method within that object `array([object[],function])` – RobertPitt Dec 28 '10 at 14:45

3 Answers3

47

To use object methods with array_map(), pass an array containing the object and the objects method name. For same-object scope, use $this as normal. Since your method name is defined in your public $func property, you can pass func.

As a side note, the parentheses outside array_map() aren't necessary.

return array_map( [$this, 'func'], range($min, $max));
James Valeii
  • 472
  • 1
  • 4
  • 14
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 5
    Just had this issue now and this didn't work. What I had to do was pass it without the $this. i.e. array_map(array($this, 'func'). Not sure why it didn't work the first way. Found it here: https://stackoverflow.com/questions/5422242/array-map-not-working-in-classes – Dan W. Jan 04 '19 at 02:28
4

The following code provides an array of emails from an $users array which contains instances of a class with a getEmail method:

    if(count($users) < 1) {
        return $users; // empty array
    }
    return array_map(array($users[0], "getEmail"), $users);
psychoslave
  • 2,783
  • 3
  • 27
  • 44
0

I tried search for solution but it not works, so I created custom small map function that will do the task for 1 variable passed to the function it simple but will act like map

class StyleService {
  function check_css_block($str){
    $check_end = substr($str,-1) == ';';
    $check_sprator =  preg_match_all("/:/i", $str) == 1;
    $check_sprator1 =  preg_match_all("/;/i", $str) == 1;
    if ( $check_end && $check_sprator && $check_sprator1){
      return $str;
    } else {
      return false;
    }
  }

  function mymap($function_name, $data){
    $result = array();
    $current_methods = get_class_methods($this);
    if (!in_array($function_name, $current_methods)){
      return False;
    }
    for ($i=0; $i<count($data); $i++){
      $function_result = $this->{$function_name}($data[$i]);
      array_push($result, $function_result);
    }
    return $result;
  }


  function get_advanced_style_data($data)

  {
    return $this->mymap('check_css_block', $data);
  }
}

this way you can call a function name as string $this->{'function_name'}() in php

Mahmoud Magdy
  • 662
  • 10
  • 13
  • 1
    I think you want [PHP - Count number of commas in string](https://stackoverflow.com/q/4116038/2943403) not `preg_match_all("/;/i", $str)` -- which by the way is nonsensical to make case-insensitive. – mickmackusa Nov 15 '22 at 22:21
  • Thanks for your comment, I've read the question you shared and added an answer to it, please check it out too thanks. – Mahmoud Magdy Nov 17 '22 at 03:51
  • My concern is not for that other page, but your answer on THIS page. Using `preg_match_all("/;/i", $str) == 1` is not ideal. There is no need to use regex for the task of counting semicolons. – mickmackusa Nov 17 '22 at 03:58