Two arrays in foreach loop

后端 未结 22 1524
不思量自难忘°
不思量自难忘° 2020-11-22 04:48

I want to generate a selectbox using two arrays, one containing the country codes and another containing the country names.

This is an example:

相关标签:
22条回答
  • 2020-11-22 05:36

    Walk it out...

    $codes = array('tn','us','fr');
    $names = array('Tunisia','United States','France');
    
    • PHP 5.3+

      array_walk($codes, function ($code,$key) use ($names) { 
          echo '<option value="' . $code . '">' . $names[$key] . '</option>';
      });
      
    • Before PHP 5.3

      array_walk($codes, function ($code,$key,$names){ 
          echo '<option value="' . $code . '">' . $names[$key] . '</option>';
      },$names);
      
    • or combine

      array_walk(array_combine($codes,$names), function ($name,$code){ 
          echo '<option value="' . $code . '">' . $name . '</option>';
      })
      
    • in select

      array_walk(array_combine($codes,$names), function ($name,$code){ 
          @$opts = '<option value="' . $code . '">' . $name . '</option>';
      })
      echo "<select>$opts</select>";
      

    demo

    0 讨论(0)
  • 2020-11-22 05:38

    Use array_combine() to fuse the arrays together and iterate over the result.

    $countries = array_combine($codes, $names);
    
    0 讨论(0)
  • 2020-11-22 05:38
    <?php
    
    $codes = array ('tn','us','fr');
    $names = array ('Tunisia','United States','France');
    
    echo '<table>';
    
    foreach(array_keys($codes) as $i) {
    
         echo '<tr><td>';
         echo ($i + 1);
         echo '</td><td>';
         echo $codes[$i];
         echo '</td><td>';
         echo $names[$i];
         echo '</td></tr>';
    }
    
    echo '</table>';
    
    ?>
    
    0 讨论(0)
  • 2020-11-22 05:38

    it works for me

    $counter = 0;
    foreach($codes as $code)
    {
    $codes_array[$counter]=$code;
    $counter++;
    }
    $counter = 0;
    foreach($names as $name)
    {
    echo $codes_array[$counter]."and".$name;
    $counter++;
    }
    
    0 讨论(0)
  • 2020-11-22 05:42

    All fully tested

    3 ways to create a dynamic dropdown from an array.

    This will create a dropdown menu from an array and automatically assign its respective value.

    Method #1 (Normal Array)

    <?php
    
    $names = array('tn'=>'Tunisia','us'=>'United States','fr'=>'France');
    
    echo '<select name="countries">';
    
    foreach($names AS $let=>$word){
        echo '<option value="'.$let.'">'.$word.'</option>';
    }
    echo '</select>';
     
    ?>
    


    Method #2 (Normal Array)

    <select name="countries">
    
    <?php
    
    $countries = array('tn'=> "Tunisia", "us"=>'United States',"fr"=>'France');
    foreach($countries as $select=>$country_name){
    echo '<option value="' . $select . '">' . $country_name . '</option>';
    }
    ?>
    
    </select>
    


    Method #3 (Associative Array)

    <?php
    
    $my_array = array(
         'tn' => 'Tunisia',
         'us' => 'United States',
         'fr' => 'France'
    );
    
    echo '<select name="countries">';
    echo '<option value="none">Select...</option>';
    foreach ($my_array as $k => $v) {
        echo '<option value="' . $k . '">' . $v . '</option>';
    }
    echo '</select>';
    ?>
    
    0 讨论(0)
  • 2020-11-22 05:43

    I solved a problem like yours by this way:

    foreach(array_keys($idarr) as $i) {
     echo "Student ID: ".$idarr[$i]."<br />";
     echo "Present: ".$presentarr[$i]."<br />";
     echo "Reason: ".$reasonarr[$i]."<br />";
     echo "Mark: ".$markarr[$i]."<br />";
    }
    
    0 讨论(0)
提交回复
热议问题