Skip to content

Instantly share code, notes, and snippets.

@TanmayChakrabarty
Last active April 6, 2024 18:36
Show Gist options
  • Save TanmayChakrabarty/e7a50cbec90be44c988cab7f4f2c8db2 to your computer and use it in GitHub Desktop.
Save TanmayChakrabarty/e7a50cbec90be44c988cab7f4f2c8db2 to your computer and use it in GitHub Desktop.
In case you need to print out hidden input fields from an array, such as from $_GET
<?php
/**
* @param array|string $input the input array. also works as the value when you have a name and a value only, not array
* @param string|null $name_prefix the name of the input field. You pass it when you have just a name and a value, not array
* @return void
*/
function generateHiddenInputFromArray(array|string $input, ?string $name_prefix = null): void {
if(is_array($input)){
foreach($input as $i=>$v){
$name = !is_null($name_prefix) ? $name_prefix.'['.$i.']' : $i;
if(is_array($v)) generateHiddenInputFromArray($v, $name);
else echo '<input type="hidden" name="'.$name.'" value="'.$v.'">';
}
}
else echo '<input type="hidden" name="'.$name_prefix.'" value="'.$input.'">';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment