/** * Corvert array data to hidden inputs * to pass as form data * * Example: [data => [sub1 => 1, sub2 => 2]] * * * * @author Ivan Gospodinow * @see http://ivangospodinow.com/?p=455 * @param array $array * @param [array | string] $exclude * @return string */ function arrayToHiddenInput(array $array, $exclude = null) { $hiddens = []; $strParams = urldecode(http_build_query($array)); if (!empty($strParams)) { $params = explode('&', $strParams); foreach ($params as $param) { list($name, $value) = explode('=', $param); if (is_array($exclude) && in_array($name, $exclude)) { continue; } else if ($name == $exclude) { continue; } $hiddens[] = sprintf( '', $name, $value ); } } return implode(PHP_EOL, $hiddens); }
Example:
echo arrayToHiddenInput(['data' => ['sub1' => 1, 'sub2' => 2]]);
Result:
Simple as that