且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何访问"键"和"价值"从传递给for循环数组?

更新时间:2022-10-23 16:04:01

我想你正在寻找的是:

 的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