PHP foreach循环使用数组数组中的值填充下拉列表

时间:2022-02-05 12:15:50

I have an array of an arrays created using the following code. I'm trying to get this to loop through and populate a dropdown list.

我有一个使用以下代码创建的数组数组。我试图让它循环并填充下拉列表。

$names = $db->fetchAll("select `name` from `classes`");

This is what is stored in the $names variable.

这是存储在$ names变量中的内容。

Array ( [0] => Array ( [name] => Web Design ) [1] => Array ( [name] => Art History ) [2] => Array ( [name] => Gym ) [3] => Array ( [name] => English ) [4] => Array ( [name] => Biology ) [5] => Array ( [name] => 3D Animation ) [6] => Array ( [name] => Tech Disc ) [7] => Array ( [name] => Math ) [8] => Array ( [name] => Dance ) [9] => Array ( [name] => Video Production ) [10] => Array ( [name] => Home Ec ) [11] => Array ( [name] => Government ) [12] => Array ( [name] => Physics ) )

数组([0] =>数组([名称] =>网页设计)[1] =>数组([名称] =>艺术史)[2] =>数组([名称] =>健身房)[3] = > Array([name] => English)[4] => Array([name] => Biology)[5] => Array([name] => 3D Animation)[6] => Array([name] = > Tech Disc)[7] => Array([name] => Math)[8] => Array([name] => Dance)[9] =>数组([name] =>视频制作)[10] => Array([name] => Home Ec)[11] => Array([name] => Government)[12] => Array([name] => Physics))

I have this dropdown list created and it work, but it is hand coded for each value in the array. I want to modify this so it 'loops' through all results in the array to create the dropdown.

我创建了这个下拉列表并且它可以工作,但它是为数组中的每个值手工编码的。我想修改它,以便“循环”遍历数组中的所有结果以创建下拉列表。

<label for="per1"></label>
<select name="per1" id="per1">
    <option selected="selected">Choose one</option>
    <option value="<?php echo $names[0]['name'];?>"><?php echo $names[0]['name'];?></option>
    <option value="<?php echo $names[1]['name'];?>"><?php echo $names[1]['name'];?></option>    
    <option value="<?php echo $names[2]['name'];?>"><?php echo $names[2]['name'];?></option>
    <option value="<?php echo $names[3]['name'];?>"><?php echo $names[3]['name'];?></option>
    <option value="<?php echo $names[4]['name'];?>"><?php echo $names[4]['name'];?></option>
    <option value="<?php echo $names[5]['name'];?>"><?php echo $names[5]['name'];?></option>
    <option value="<?php echo $names[6]['name'];?>"><?php echo $names[6]['name'];?></option>
    <option value="<?php echo $names[7]['name'];?>"><?php echo $names[7]['name'];?></option>
    <option value="<?php echo $names[8]['name'];?>"><?php echo $names[8]['name'];?></option>
    <option value="<?php echo $names[9]['name'];?>"><?php echo $names[9]['name'];?></option>
    <option value="<?php echo $names[10]['name'];?>"><?php echo $names[10]['name'];?></option>
    <option value="<?php echo $names[11]['name'];?>"><?php echo $names[11]['name'];?></option>
    <option value="<?php echo $names[12]['name'];?>"><?php echo $names[12]['name'];?></option>           
</select> 

Can someone please help?

有人可以帮忙吗?

3 个解决方案

#1


14  

This solution works for current PHP versions. Simple case of using a foreach:

此解决方案适用于当前的PHP版本。使用foreach的简单情况:

<select name="per1" id="per1">
  <option selected="selected">Choose one</option>
  <?php
    foreach($names as $name) { ?>
      <option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
  <?php
    } ?>
</select> 

This solution works for older PHP versions. Simple case of using a foreach:

此解决方案适用于较旧的PHP版本。使用foreach的简单情况:

<select name="per1" id="per1">
  <option selected="selected">Choose one</option>
  <?php
    foreach($names as $name) { ?>
      <option value="<?php echo $name['name'] ?>"><?php echo $name['name'] ?></option>
  <?php
    } ?>
</select> 

#2


2  

i think this is enough

我认为这就足够了

foreach($names as $key =>$value)
{?>
<option value="<?=$value['name']?>"><?=$value['name']?></option>    
<?php }

#3


0  

Go for for each loop to avoid these much coding.

寻找每个循环以避免这些编码。

<?php 
foreach($names as $nameIndex=>$nameVal){ ?>
   <option value=$nameVal['name']?>"><?=$nameVal['name']?> </option>
<?php } ?>

#1


14  

This solution works for current PHP versions. Simple case of using a foreach:

此解决方案适用于当前的PHP版本。使用foreach的简单情况:

<select name="per1" id="per1">
  <option selected="selected">Choose one</option>
  <?php
    foreach($names as $name) { ?>
      <option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
  <?php
    } ?>
</select> 

This solution works for older PHP versions. Simple case of using a foreach:

此解决方案适用于较旧的PHP版本。使用foreach的简单情况:

<select name="per1" id="per1">
  <option selected="selected">Choose one</option>
  <?php
    foreach($names as $name) { ?>
      <option value="<?php echo $name['name'] ?>"><?php echo $name['name'] ?></option>
  <?php
    } ?>
</select> 

#2


2  

i think this is enough

我认为这就足够了

foreach($names as $key =>$value)
{?>
<option value="<?=$value['name']?>"><?=$value['name']?></option>    
<?php }

#3


0  

Go for for each loop to avoid these much coding.

寻找每个循环以避免这些编码。

<?php 
foreach($names as $nameIndex=>$nameVal){ ?>
   <option value=$nameVal['name']?>"><?=$nameVal['name']?> </option>
<?php } ?>