Skip to content

Instantly share code, notes, and snippets.

@intel352
Created January 29, 2012 03:00
Show Gist options
  • Save intel352/1696924 to your computer and use it in GitHub Desktop.
Save intel352/1696924 to your computer and use it in GitHub Desktop.
CHtml::hiddenFields
<?php
# In response to: https://www.facebook.com/groups/61355672149/10150626399467150/
$hiddenFields = array('field1'=>'value1', 'field2'=>'value2');
array_map('echo', array_map(array('CHtml', 'hiddenField'), array_keys($hiddenFields), $hiddenFields));
# OR same solution as previous, but with the addition of common html options
$hiddenFields = array('field1'=>'value1', 'field2'=>'value2');
$htmlOptions = array('customAttribute'=>'someValue');
array_map('echo', array_map(array('CHtml', 'hiddenField'), array_keys($hiddenFields), $hiddenFields, array_fill(0, count($hiddenFields), $htmlOptions)));
# OR similar solution as previous, but using a closure & removing the additional array_map
# closures require PHP 5.3+
$hiddenFields = array('field1'=>'value1', 'field2'=>'value2');
$htmlOptions = array('customAttribute'=>'someValue');
array_map(function($field, $value)use($htmlOptions){echo CHtml::hiddenField($field, $value, $htmlOptions)}, array_keys($hiddenFields), $hiddenFields);
# OR similar solution as previous, array_walk instead of array_map
$hiddenFields = array('field1'=>'value1', 'field2'=>'value2');
$htmlOptions = array('customAttribute'=>'someValue');
array_walk($hiddenFields, function($value, $field)use($htmlOptions){echo CHtml::hiddenField($field, $value, $htmlOptions)});
# OR similar solution as previous, but shorter without html options
$hiddenFields = array('field1'=>'value1', 'field2'=>'value2');
array_walk($hiddenFields, function($value, $field){echo CHtml::hiddenField($field, $value)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment