Skip to content

Instantly share code, notes, and snippets.

@sherbrow
Last active August 29, 2015 14:26
Show Gist options
  • Save sherbrow/fb1495dfd2eb9954df29 to your computer and use it in GitHub Desktop.
Save sherbrow/fb1495dfd2eb9954df29 to your computer and use it in GitHub Desktop.
Form hidden fields equivalent of http_build_query
/**
* First draft of sister function http_build_query http://php.net/manual/function.http-build-query.php
* intended to generate hidden form inputs from the *same* arguments
*/
function html_build_form(array $array, $prefix = '') {
$str = '';
foreach($array as $key => $val) {
$name = $prefix?$prefix.'['.$key.']':$key;
if(is_array($val)) $str .= html_build_form($val, $name);
else {
$str .= '<input type="hidden" name="'.htmlentities($name).'" value="'.htmlentities($val).'">'; // use htmlentities($name,ENT_COMPAT,'UTF-8'); if server encoding differs from text
}
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment