PHP Function Argument to an array

后端 未结 4 440
时光说笑
时光说笑 2021-01-28 00:59

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_parama         


        
相关标签:
4条回答
  • 2021-01-28 01:06

    do you mean

    function valuesToArray($value1,$value2){return array($value1,$value2);}
    
    0 讨论(0)
  • 2021-01-28 01:11

    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'));
    
    0 讨论(0)
  • 2021-01-28 01:26

    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);
    
    0 讨论(0)
  • 2021-01-28 01:27

    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)
提交回复
热议问题