symfony: question about a choice widget
I have a choice widget in a form:
$years = range(14,130);
$this->widgetSchema['age'] = new sfWidgetFormSchema();
foreach (array('from', 'to') as $value)
{
$this->widgetSchema['age'][$value] = new sfWidge开发者_运维百科tFormChoice(array(
'label' => $value,
'choices' => array_merge(array('' => '-'),array_combine($years,$years))));
}
If i choose for example 14 and in the action that receives the form is written something like this:
var_dump($valores_widgets['age']['from']);
that shows 0. But I expected 14.
Any idea?
Regards
Javi
Check the documentation of array_merge
:
If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If all of the arrays contain only numeric keys, the resulting array is given incrementing keys starting from zero.
A quick test:
maerlyn@biliskner:~$ php --run '$years=range(14,16);var_dump(array_merge(array("" => "-"), array_combine($years,$years)));'
array(4) {
[""]=>
string(1) "-"
[0]=>
int(14)
[1]=>
int(15)
[2]=>
int(16)
}
So your $years array gets reindexed during the merge. When using the +
operator instead:
maerlyn@biliskner:~$ php --run '$years=range(14,16);var_dump(array("" => "-") + array_combine($years,$years));'
array(4) {
[""]=>
string(1) "-"
[14]=>
int(14)
[15]=>
int(15)
[16]=>
int(16)
}
精彩评论