tags:

views:

92

answers:

7

I could not figure out how to pass a variable number of variables into a function. I thought passing in an array and using the array keys for the variables names could replace the need to pass extra variables into the function, and it worked (I'm sure there is a better way to accomplish this, suggestions welcome). However, I can't seem to get the keys out of the array inside the function.

The array:

  $parameters[day] = 1;
  $parameters[month] = 8;
  $parameters[year] = 2010; 

Inside the function:

foreach(key($parameters) as $key)
{
   print($key);
   print("<br>");
}

The code inside the function retuns a warning: Invalid argument supplied for foreach(). How can I pull the keys out of the array?

+3  A: 

Passing an associative array to a function is a reasonable way to pass in a variable number of parameters.

foreach ($parameters as $key => $value) {
  echo $key . ' = ' . $value . '<br>';
}

Alternatively you could pass in an instance of stdClass (casting the argument to an object). But an array does the job.

I assume your array keys aren't constants, in which case they should be quoted strings:

$parameters['day'] = 1;
$parameters['month'] = 8;
$parameters['year'] = 2010; 
w3d
+1 - The code in the first block helped clear up confusion I had from a typo in the answer I accepted. Also thanks for commenting on the acceptability of using this method for passing the variables to a function.
JMC
You might also like to read [this recent thread about passing a variable number of vars as separate vars](http://stackoverflow.com/questions/3358216/php-is-it-possible-to-convert-array-to-vars-and-pass-them-somewhere) with the help of the `func_get_args()` functions that @Rob mentions above. However, the associative array method (like you are doing) is better IMO.
w3d
+1  A: 

The array_keys function will return an array of all the array keys.

kunal
+6  A: 

You can use PHP's array_keys function to grab the keys, like so:

foreach(array_keys($parameters) as $paramName)
  echo $paramName . "<br>";

Or, you can run through the array using a special foreach which allows you to separate the key and value for every element, like so:

foreach($parameters as $paramName => $value)
  echo $paramName . "<br>";

Also, make sure that you are using a "string" (with quotes) or integer (like 1337) as your key, like so:

$parameters["day"] = 1;
$parameters["month"] = 8;
$parameters["year"] = 2010;

OR if you want to get fancier:

$parameters = array(
  "day" => 1,
  "month" => 8,
  "year" => 2010
);

Your code should look like:

$parameters = array(
  "day" => 1,
  "month" => 8,
  "year" => 2010
);
foreach($parameters as $paramName => $paramValue)
  echo $paramName . "<br>";
Garrett
Good thorough example code. I ended up using this to the solve the problem. There are a few typos in the code blocks, you might want to edit for anyone that stumbles on this later. (Example: I think $key should be $param in the first block. I think "<br> should be $value in the second block. and $parameters is misspelled in the last block.) Thanks for putting this together.
JMC
Thank you. I was unsure whether you wanted to print the values or do something else with them, but I have made the corrections. No problem =)
Garrett
A: 
foreach(array_keys($parameters) as $key) {
   echo $key.'<br/>';
}
kgb
+2  A: 

try this

foreach($parameters as $key => $value)
jordanstephens
+3  A: 

I'm sure there is a better way to accomplish this, suggestions welcome

Because you asked for alternate suggestions, here's one. You can use varargs to pass a variable number of arguments to a function. Here's an example:

function my_function() {
    $numArgs = func_num_args();
    $args = func_get_args(); // an array of the arguments, in order
}

This doesn't offer you "named" arguments, but it does allow you variable numbers of arguments (like you claim you're trying to do in your question).

Here are some relevant documentation pages:

However, that's not to say that your array-based approach isn't a bad one. In some ways it provides batter readability since you're explicitly mapping keys to values; maintainers reading your code will be better-able to understand what's being passed to the function. I'm just giving you some options.

Rob Hruska
@Rob Hruska: Nice to see that. I was thinking whether PHP has similar vararg implementation like Java's :)
Michael Mao
+1 - For posting an alternative. Thanks
JMC
A: 
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
?>