0

i need to know how to pass an array to a function as separate variables, for instance,

function myfunction($var, $othervar) {
}

$myarray = array('key'=>'val', 'data'=>'pair');

here is where I am running into problems, the following doesn't seem to work:

$return = myfunction(extract($myarray));

it should, if I understand correctly, basically be the same as

$return = myfunction($key, $data);

where $key='val' and $data='pair'

can anyone please explain this to me.

4
  • 2
    You're looking for call_user_func_array() Commented Jul 31, 2013 at 3:48
  • 1
    possible duplicate of PHP: Array to variable function parameter values Commented Jul 31, 2013 at 3:49
  • what about this: it was: $this->_obj[$key]->$function(extract($vars)); now it's: return call_user_func_array(array(get_class($this->_obj[$key]), $function), $vars); but this doesn't work because a non-static method 'can't' be called statically Commented Jul 31, 2013 at 3:58
  • given what you see above, how would you implement array_walk here? Commented Jul 31, 2013 at 4:02

3 Answers 3

0

If I am understanding your question right then try this

$return = myfunction($myarray["key"],$myarray["data"]);

here we are simply passing the associative array as arguments.

Sign up to request clarification or add additional context in comments.

2 Comments

that's useful but the problem there is that I don't know how many variables there are or what the keys are, i'll post the complete code in an answer below
should i just ignore the 'Strict Standards' error message and use call_user_func_array?
0
    public function get($key=null, $function='', array $vars=array()) {
        if ($key==null || !array_key_exists($key, $this->_obj)) {
            return null;
        }

var_dump($vars);

        if (!empty($function)) {
            return $this->_obj[$key]->$function(array_walk($vars));
//          return call_user_func_array(array(get_class($this->_obj[$key]), $function), $vars);
//          return $this->_obj[$key]->$function(extract($vars));
        }
        return $this->_obj[$key];
    }

2 Comments

now with call_user_func_array I'm getting this: Using $this when not in object context in C:\aDomain\profordable\lib\Class_Database.php on line 42 because apparently using call_user_function_array(array('class', 'function'), $vars) with an array as the function name, calls the method statically
I was passing the class in call_user_func_array as a string instead of as an object, what i have now is below, and it seems to work, although more input would be appreciated
0
    public function get($key=null, $function='', array $vars=array()) {
        if ($key==null || !array_key_exists($key, $this->_obj)) {
            return null;
        }
        if (!empty($function)) {
            return call_user_func_array(array($this->_obj[$key], $function), $vars);
        }
        return $this->_obj[$key];
    }

$registry = Registry_Object::Singleton();
//...later on
$registry = $GLOBALS['registry'];
$registry->set('Content', new Content($_SERVER['REQUEST_URI']));
$id = $registry->get('Content', 'GetIdFunc');
$registry->set('DB', 'Database');
$query = $registry->get('DB', 'Read', array("SELECT TITLE, CONTENT FROM APP_CONTENT WHERE ID=$id"));
print '<h1>'.$query[0]['TITLE'].'</h1>';
print Template_Helper::TPL_Paragraphs($query[0]['CONTENT']);

thanks all

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.