Scott B
Scott B

Reputation: 40157

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

How can I change the foreach loop below so that I can assign the $myradiooption array's key 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)?

<?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;

Upvotes: 5

Views: 28500

Answers (5)

bharath
bharath

Reputation: 1233

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>

Upvotes: 1

John Parker
John Parker

Reputation: 54445

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.

Upvotes: 5

Nanne
Nanne

Reputation: 64399

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

Upvotes: 8

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

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

References: foreach in the PHP documentation

Upvotes: 9

Reinderien
Reinderien

Reputation: 15221

This should explain it pretty well:

http://www.tech-recipes.com/rx/295/php-syntax-iterate-over-an-associative-array/

Upvotes: -1

Related Questions