php 如何从传递给 for 循环的数组中访问“键”和“值”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4784419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:21:45  来源:igfitidea点击:

How to access "key" and "value" from an array passed to a for loop?

phparraysloops

提问by Scott B

How can I change the foreachloop below so that I can assign the $myradiooptionarray's keyas the valuefor each input instead of the array's optionvalue as I'm now doing (I still want to echo the array's optionvalue as the label)?

我怎样才能改变foreach循环之下,这样我可以指定$myradiooption数组keyvalue每个输入而非阵列的option价值,因为我现在做的(我还是想呼应数组option作为标签值)?

<?php
$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 )
);
//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;

回答by Nanne

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

现在您可以访问密钥为$key,选项为$option

回答by Sebastian Paaske T?rholm

If you want to access the key of an array in a foreach loop, you use the following syntax:

如果要在 foreach 循环中访问数组的键,请使用以下语法:

foreach ($array as $key => $value) { ... }

References: foreachin the PHP documentation

参考资料:foreach在 PHP 文档中

回答by John Parker

If you want to extract key/value pairs from an associative array, simply use...

如果要从关联数组中提取键/值对,只需使用...

foreach ($yourArray as $key => $value) {
   ...
}

...as per the PHP foreach manual page.

...根据PHP foreach 手册页

回答by bharath

the whole thing can be changed to something like this for better readability...

为了更好的可读性,整个事情都可以改成这样……

<?php

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

$value = array(  
            "name" => "Category Layout",
            "desc" => "description goes here",
            "id" => "my_category_layout",
            "type" => "radio",
            "options" => $myradiooptions 
         );

foreach($value as $key => $val)
{
    $formHTML = "<label class='left' for='{$value['id']}'>".$value['name']."</label>";
    if(is_array($val))
    {
        $count = 1;
        foreach($val as $k => $v)
        {
            $formHTML .= "<input type='radio' name='{$v['id']}' id='$count' value='$v' /><label style='color:#666; margin:0 20px 0 5px;' for='$count'>$v</label>";
            $count ++;
        }
    }
    $formHTML .= "<label class='description' style='margin-top:-5px;'>".$value['desc']."</label>";
}

//switch, case "radio":
?>
<li class="section">
    <?php print $formHTML; ?>
</li>