将 $$k[0] 的 PHP 计算从 ${$k[0]} 更改为 ${$k}[0]
尽管我在上面使用 $$ 将字符串视为变量,但我这里有一段代码不起作用:
<? foreach (KOHANA::config('list.amenities_forms') as $k => $v) : ?>
<div class="form">
<fieldset>
<legend><?php echo $v ?></legend>
<input type="checkbox" name="<?=$k?>flag" id="<?=$k?>flag"/>
<label class="inline"><?=$v?></label>
<label>Description</label>
<textarea cols="50" rows="5" name="<?=$k?>[]"><?= empty($$k[0]) ? '' : $$k[0]?></textarea>
<label>Size</label>
<input type="text" name="<?=$k?>[]" value="<?= empty($$k[1]) ? '' : $$k[1]?>"/>
<label>Capacity</label>
<input type="text" name="<?=$k?>[]" value="<?= empty($$k[2]) ? '' : $$k[2]?>"/>
</fieldset>
</div>
<? endforeach?>
函数 Kohana::config 返回此数组:
'amenities_forms' => array(
'meeting_space' => 'Meeting Space',
'breakfast_room' => 'Breakfast Room',
'living_quarters' => 'Living Quarters',
'restaurant' => 'Restaurant',
'bar' => 'Bar'
)
我可能做错了什么?
I have a piece of code here that does not work despite me using a $$ on it to treat the string as a variable:
<? foreach (KOHANA::config('list.amenities_forms') as $k => $v) : ?>
<div class="form">
<fieldset>
<legend><?php echo $v ?></legend>
<input type="checkbox" name="<?=$k?>flag" id="<?=$k?>flag"/>
<label class="inline"><?=$v?></label>
<label>Description</label>
<textarea cols="50" rows="5" name="<?=$k?>[]"><?= empty($k[0]) ? '' : $k[0]?></textarea>
<label>Size</label>
<input type="text" name="<?=$k?>[]" value="<?= empty($k[1]) ? '' : $k[1]?>"/>
<label>Capacity</label>
<input type="text" name="<?=$k?>[]" value="<?= empty($k[2]) ? '' : $k[2]?>"/>
</fieldset>
</div>
<? endforeach?>
the function Kohana::config returns this array:
'amenities_forms' => array(
'meeting_space' => 'Meeting Space',
'breakfast_room' => 'Breakfast Room',
'living_quarters' => 'Living Quarters',
'restaurant' => 'Restaurant',
'bar' => 'Bar'
)
What could I be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于 PHP 将
$$k[0]
解释为使用变量$k[0]
中的字符串作为变量的名称,当您只想使用$k
变量的内容作为变量的名称。使用${$k}[0]
代替,应该使 PHP 理解您想要做什么,而不是使用数组索引作为$k
变量的一部分。例如,
这将输出“bar”,但如果没有花括号,它将无法工作。
I think the problem is the fact that PHP interprets
$$k[0]
as using the string from the variable$k[0]
as the name of the variable, when you wanted to only use the contents of the$k
variable as name of the variable. Using${$k}[0]
instead, should make PHP understand what you wanted do and not use the array index as part of the$k
variable.For example,
This will output "bar", but it would not work without the curly braces.