Is there a way to make the arguments of a function act as an array? I’m finding this difficult to explain.
Here’s kind of an example.. When you declare an array, you can define the keys => values like so:
$array = array(
"key" => "value",
"other_key" => "other_value"
);
And if I make a function that for an example outputs these onto the document, I could have:
function write($ar)
{
foreach($ar as $key => $value)
echo "$key: $value<br />";
}
write($array); // parse previously mentioned array
What I want to be able to do is omit the need to parse an array like above or below examples..
write(array(
"key" => "value",
"other_key" => "other_value"
));
I know I can use func_get_args() to list any amount of arguments, but is there a similar function that lets you parse key => value pairs rather than just a list of values?
Hope I described this in a way that makes sense, what I essentially want to end up with is something like:
write(
"key" => "value",
"other_key" => "other_value"
);
A function cannot take the argument as an array structure, so your best bet would be to use the method you specified in your second to last example:
You could then have a default array within your function (if desired) so you could merge the two together so you always have a decent set of data.
EDIT
Unless you want to go crazy and pass it through as a string:
And then parse that out on the other side… but IMO I wouldn’t bother, potentially opening yourself up to issues here.