2012-04-22 57 views
6

是否可以有一個數組並將其作爲單獨的參數傳遞給一個函數?你可以將數組插入函數參數嗎?

$name = array('test', 'dog', 'cat'); 
$name = implode(',' $name); 
randomThing($name); 

function randomThing($args) { 
    $args = func_get_args(); 
    // Would be 'test', 'dog', 'cat' 

    print_r($args); 
} 

回答

0

從PHP 5.6起,您可以使用...傳遞數組作爲函數參數。從PHP文檔中看到這個example

function add($a, $b) { 
    return $a + $b; 
} 

echo add(...[1, 2])."\n"; 

$a = [1, 2]; 
echo add(...$a); 
相關問題