本文介绍了如何访问"键"和"价值"从传递给for循环数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何更改下方的foreach循环,这样我可以分配$ myradiooption阵列的钥匙价值的价值为每个输入而非阵列的选项值,因为我现在做的(我还是想呼应阵列的选项值作为标签)?
$ myradiooptions =阵列(GRID1=>中网格视图(默认),list1的=>中列表视图(1列),列表2= >中列表视图(2列));阵列(名= GT;分类布局,
说明=> 介绍到这里,
ID=> my_category_layout
类型=> 无线电,
选项=> $ myradiooptions) ...等等}//开关案电台:
?>
<李班=部分>
<标签类=左为=< PHP的echo $值['身份证'];?>>< PHP的echo $值['名']; ?>< /标签>
< PHP $计数= 1;的foreach($值['选项']为$选项){&GT?;
<输入类型=电台NAME =< PHP的echo $值['身份证'];?>中ID =< PHP的echo $计数;>中值=< PHP的echo $选项;>中< PHP检查($选项,get_settings($值['身份证'])); ?> /><标签样式=COLOR:#666;保证金:0 0 20px的为5px;对于=< PHP的echo $计数;?>>< PHP的echo $选项; ?>< /标签>
< PHP $计数++;}&GT?;
<标签类=说明的风格=的margin-top:-5px;?>< PHP的echo $值['说明']; ?>< /标签>
< /李>
< PHP
打破;
解决方案
我想你正在寻找的是:
的foreach($值['选项']为$关键=> $选项)
现在可以访问密钥 $键
,选项为 $选项
How can I change the "foreach" loop below so that I can assign the $myradiooption array's "key" value as the "value" for each input instead of the array's "option" value as I'm now doing (I still want to echo the array's "option" value as the label)?
$myradiooptions = array("grid1" => "Grid View (default)", "list1" => "List View (1 column)", "list2" => "List View (2 column)" );
array( "name" => "Category Layout",
"desc" => "description goes here",
"id" => "my_category_layout",
"type" => "radio",
"options" => $myradiooptions ),
...etc}
//switch, case "radio":
?>
<li class="section">
<label class="left" for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
<?php $count=1;foreach ($value['options'] as $option) { ?>
<input type="radio" name="<?php echo $value['id']; ?>" id="<?php echo $count; ?>" value="<?php echo $option; ?>" <?php checked($option, get_settings($value['id'])); ?>/><label style="color:#666; margin:0 20px 0 5px;" for="<?php echo $count; ?>"><?php echo $option; ?></label>
<?php $count++;} ?>
<label class="description" style="margin-top:-5px;"><?php echo $value['desc']; ?></label>
</li>
<?php
break;
解决方案
I think what you are looking for is this:
foreach ($value['options'] as $key=>$option)
Now you can access the key as $key
, and the option as $option
这篇关于如何访问&QUOT;键&QUOT;和&QUOT;价值&QUOT;从传递给for循环数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!