I'm building a basic form building class to speed my workflow up a bit and I'd like to be able to take an array of attributes like so:
$attributes = array(
"type" => "text",
"id" => "contact-name",
"name" => "contact-name",
"required" => true
);
<input type="text" id="contact-name" name="contact-name" required />
I think this should do it:
$result = '<input '.join(' ', array_map(function($key) use ($attributes)
{
if(is_bool($attributes[$key]))
{
return $attributes[$key]?$key:'';
}
return $key.'="'.$attributes[$key].'"';
}, array_keys($attributes))).' />';