Skip to content

Instantly share code, notes, and snippets.

@r17x
Last active November 16, 2017 04:40
Show Gist options
  • Save r17x/e3a4f00249e0a7e00227a78ae34f9e43 to your computer and use it in GitHub Desktop.
Save r17x/e3a4f00249e0a7e00227a78ae34f9e43 to your computer and use it in GitHub Desktop.
arrayToHtmlAttributes.php
<?php
/*
* @author ri7nz <[email protected]>
*/
function toAttributes($arr){
$arr = array_map(
function($value, $key){
return $key.'="'.$value.'"';
},
array_values($arr),
array_keys($arr)
);
return implode(' ', $arr);
}
$attr = array(
'id' => 'input1',
'class' => 'form-control',
'name' => 'input1'
);
$attr = toAttributes($attr);
echo "<input type='text' ${attr} />";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment