简体   繁体   English

PHP函数参数到数组

[英]PHP Function Argument to an array

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)

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 ): 实际上,这非常简单(请阅读手册: func_get_args —返回包含函数的参数列表的数组,另请参见: 可变长度参数列表 ):

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: Reflection Docs是您的朋友:

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

I'm not sure if you're looking for func_get_args which return all arguments passed to a function, or the ReflectionFunction class. 我不确定是否要查找func_get_args ,该函数返回传递给函数或ReflectionFunction类的所有参数。

A basic example of func_get_args: 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;
}

你的意思是

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM