0

Can you do something crazy like this

function cool_function($pizza, $ice_cream) { 

   make the arguments in the array into an array 
   return $array_of_paramaters // my new array 

} // end function 

print_r(cool_function); // outputs array(0 => pizza, 1=> ice cream)
2
  • Why would you need something like this? Commented Sep 29, 2011 at 21:21
  • 2
    Sure you can do something like that. Why do you want to do it? Although your example: print_r(cool_function); will not work. Commented Sep 29, 2011 at 21:22

4 Answers 4

4

Actually, this is pretty easy (and read the manual: func_get_args — Returns an array containing a function's argument list, see as well: Variable-length argument lists):

function cool_function($pizza, $ice_cream) { 
   return func_get_args();
}

but as asked in comments, why do you need this?

Or do you need the variable names? Reflection Docs is your friend:

function cool_named($neutron, $electron)
{
    $f = new ReflectionFunction(__FUNCTION__);
    $p = array();    
    foreach($f->getParameters() as $p1)
        $p[] = '$'.$p1->name;

    return $p;
}

var_dump(cool_named());

Or just both? (Edit: taking under-length, optional and over-length parameters into account):

function overall($neutron, $electron, $quark = 'xiaro')
{
    $f = new ReflectionFunction(__FUNCTION__);
    $defined = $f->getParameters();
    $passed = func_get_args() + array_fill(0, count($defined), NULL);

    foreach($defined as &$param)
        $param = '$'.$param->name;

    return array_combine($defined + array_keys($passed), $passed);
}

var_dump(overall('clara', 'peter', 'moon', 'jupiter'));
3

I'm not sure if you're looking for func_get_args which return all arguments passed to a function, or the ReflectionFunction class.

A basic example of func_get_args:

function cool_function($pizza, $ice_cream) { 
   return func_get_args();
}

But you don't need the arguments to make this work:

function cool_function() { 
   return func_get_args();
}
// cool_function(1,2,3) will return array(1,2,3)

The reflection option:

/**
 * Returns an array of the names of this function
 */
function getMyArgNames($a,$b,$c)
{
    $args = array();
    $refFunc = new ReflectionFunction(__FUNCTION__);
    foreach( $refFunc->getParameters() as $param ){
         $args[] = $param->name;
    }
    return $args;
}

Or, for the really crazy:

/**
 * Returns an associative array of the names of this function mapped 
 * to their values
 */
function getMyArgNames($a,$b,$c)
{
    $received = func_get_args();
    $i = 0;
    $args = array();
    $refFunc = new ReflectionFunction(__FUNCTION__);
    foreach( $refFunc->getParameters() as $param ){
         $args[$param->name] = $received[$i++];
    }
    // include all of those random, optional arguments.
    while($i < count($received)) $args[] = $received[$i++];
    return $args;
}
0
0

do you mean

function valuesToArray($value1,$value2){return array($value1,$value2);}
0

If I got you right, you so it like this:

function cool_function($pizza, $ice_cream) {
   return array($pizza, $ice_cream);
}

but you could simply do that:

$new_var = array($pizza, $ice_cream);

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.