I'm trying to figure out a better/cleaner way to have something like this in PHP:
// This will be parsed as the only argument in a function...
$params = array('Name', 'Age', 'Mail' => '[email protected]');
"Name" and "Age" are values with automatic keys (0 and 1, eg.), and "Mail" is a key with "[email protected]" value:
[0] => 'Name',
[1] => 'Age',
['Mail'] => '[email protected]'
When running through it in a foreach loop, to have "Name" and "Age" as the actual parameters I'm using this:
foreach ($params as $k => $i) {
// This is the ugly part!
if (is_int($k)) {
$k = $i;
$i = false;
}
// To do something like this, for example...
$out = "";
if ($i) {
$out .= "<p><a href=\"mailto:$i\">$k</a></p>\n";
} else {
$out .= "<p>$k<p>\n";
}
}
That will return something like this:
<p>Name</p>
<p>Age</p>
<p><a href="mailto:[email protected]">Mail</a></p>
Is there a better way to do it?
Thanks in advance.
Edit #1: Elaborating the question: is there a clean PHP way to distinguish elements that have explicitly informed keys from the ones that have not in the same array?