how to expand an array elements as separate parameters to a function how to expand an array elements as separate parameters to a function php php

how to expand an array elements as separate parameters to a function


Since PHP 5.6 you can use argument unpacking with the triple-dot-operator:

setLineColor(...$colors);


You can use the function call_user_func_array.

<?php$colors = array('red','maroon','blue','green');call_user_func_array('setLineColor', $colors);?>

If you want to call the method of an object, you can use this instead:

<?php$graph = new ...$colors = array('red','maroon','blue','green');call_user_func_array(array($graph, 'setLineColor'), $colors);?>


function($color[0], $color[1], $color[2], $color[3])