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
)
2
  • It should work if you just wrote the function name there, "doSomeWork" Commented Dec 28, 2010 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]) Commented Dec 28, 2010 at 14:45

3 Answers 3

49

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));
Sign up to request clarification or add additional context in comments.

1 Comment

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: stackoverflow.com/questions/5422242/…
3

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);

1 Comment

This will extract the email of the user[0] as many times as users you have.
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

3 Comments

I think you want PHP - Count number of commas in string not preg_match_all("/;/i", $str) -- which by the way is nonsensical to make case-insensitive.
Thanks for your comment, I've read the question you shared and added an answer to it, please check it out too thanks.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.