这里我有一个数组,我想使用php确认第一个键值默认值
我的数组--- print_r($ compoItem);
Array
(
[0] => Rice+chapathi
[1] => Chappathi
[2] => Pepsi
[3] => 7up
)
我的HTML
<?php
$compoItem = explode(',',$sql2['combo']);
foreach($compoItem as $comValues)
{
?>
<div class="radio">
<ul><input type="radio" name="optradio" checked=""><?php echo $comValues?></ul>
</div>
<?php } ?>
这里最后的keu值是deafult check但是对我来说不想要最后一个值我想要键值是默认选中的,怎么能这样做?
答案 0 :(得分:1)
根据数组中的索引回显属性。
<?php
$compoItem = explode(',',$sql2['combo']);
foreach($compoItem as $i => $comValues){
// ---^^^^^---
?>
<div class="radio">
<ul><input type="radio" name="optradio" <?php if($i == 0){ echo 'checked="checked"'; }?>><?php echo $comValues?></ul>
</div>
<?php } ?>
答案 1 :(得分:0)
<?php
$compoItem = explode(',',$sql2['combo']);
$first = true;
foreach ( $compoItem as $comValues )
{
$checked = ( $first ) ? 'checked="checked"' : "";
?>
<div class="radio">
<ul><input type="radio" name="optradio" <?php echo $checked; ?>><?php echo $comValues?></ul>
</div>
<?php
$first = false;
} ?>