jsuissa
jsuissa

Reputation: 1762

Passing variable arguments in PHP to one array without array declaration

In PHP is there a way to pass variable arguments as a single array without requiring an array declaration?

I'm calling a function below like:

$this->layouts->add_include(array('css/style.css','css/internal.css'));

Is there any way in the function to make it accept multiple variable arguments without needing to declare the array like:

$this->layouts->add_include('css/style.css','css/internal.css');

If I do this above then I get the following error in my functions foreach loop using the second code sample:

 Invalid argument supplied for foreach()

For reference the function is:

public function add_include($array)     
{
$asset_path = 'assets/';
  foreach($array as $array_item) {

    $this->CI->load->helper('url'); // Just in case!
    $this->includes[] = base_url() . $asset_path . $array_item;
}

Upvotes: 0

Views: 439

Answers (1)

Scuzzy
Scuzzy

Reputation: 12332

func_get_args() can be used here

public function add_include()     
{
  $asset_path = 'assets/';
  foreach(func_get_args() as $array_item) {

    $this->CI->load->helper('url'); // Just in case!
    $this->includes[] = base_url() . $asset_path . $array_item;
}

Upvotes: 1

Related Questions