如何从for循环中的数组访问‘key’和‘Value’?

时间:2011-01-24 作者:Scott B

如何更改下面的for循环,以便将数组的“key”值指定为每个输入的“value”,而不是像我现在所做的那样将数组的“option”值指定为“value”(我仍然希望将数组的“option”值作为标签进行回显)?

$myradiooptions = array("grid1" => "Grid View (default)", "list1" => "List View (1 column)", "list2" => "List View (2 column)" );

$options = array (
array( "name" => "My Theme Options","type" => "title"), array( "type" => "open"),

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;

1 个回复
最合适的回答,由SO网友:Backie 整理而成

如果我对你的理解是正确的,就改变一下

<?php $count=1;foreach ($value[\'options\'] as $key => $option) { ?>
您可以从$key访问密钥。

结束

相关推荐

当我试图停用任何插件时,它会给出错误“WARNING:CALL_USER_FUNC_ARRAY()[Function.Call-User-Func-ARRAY]”

当我尝试取消激活任何插件时,它会出错。如何解决这个问题?插件被取消激活,但单击按钮“Deactivate”时,我收到此错误。警告:call\\u user\\u func\\u array()[函数.call user func array]:第一个参数应该是有效的回调,“youtuber\\u uninstall”在/home/username/public\\u html/wp includes/plugin中给出。php在线395**