Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多次显示值的下拉列表_Php_Html_Arrays - Fatal编程技术网

Php 多次显示值的下拉列表

Php 多次显示值的下拉列表,php,html,arrays,Php,Html,Arrays,我在php中有一个数组-我正在使用下面的代码为这个数组创建一个下拉列表- <html> <head> </head> <body> <form method="post" action="test.php"> <select name="feature" id="Feature"> <?php $i=0; foreach($newFeature as $feat)

我在php中有一个数组-我正在使用下面的代码为这个数组创建一个下拉列表-

<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<select name="feature" id="Feature">
        <?php
        $i=0;
        foreach($newFeature as $feat)
        {
            ?>
        <option value="<?php echo $feat;?>"><?php echo $newFeature[$i];?></option>
            <?php
        $i++;
       }
         ?>
</select> <input type="submit" name="submit" value="Test">
</form>
</body>
</html>


显示$newFeature数组。还有,为什么需要$i变量

$newFeature[$i]

这里有一种更简洁的方法来生成列表,而且您对每个列表都使用了错误的方法

<form method="post" action="test.php">
    <select name="feature" id="Feature">
    <?php foreach($newFeature as $feat): ?>
        <option value="<?php echo $feat; ?>"><?php echo $feat; ?></option>
    <?php endforeach; ?>
    </select> 
    <input type="submit" name="submit" value="Test">            
</form>


在我看来,你有点把foreach和for循环搞混了

在你的例子中,说$newFeature[$i]或$feat实际上是指同一件事

因此,请尝试以下方法:

<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<select name="feature" id="Feature">
        <?php
        foreach($newFeature as $feat)
        {
            ?>
        <option value="<?php echo $feat;?>"><?php echo $feat;?></option>
            <?php
       }
         ?>
</select> <input type="submit" name="submit" value="Test">
</form>
</body>
</html>


如果您使用的是
foreach
无需计数,您应该改用
$feat

<?php
foreach($newFeature as $feat)
{
    ?>
<option value="<?php echo $feat;?>"><?php echo $feat;?></option>
    <?php
   }
?>


使用$newFeature[$i]是使用foreach循环的错误方法。 请发布阵列的快照或尝试以下操作

调试并纠正问题

  • 在使用foreach之前打印$newFeatures数组并调试代码

  • 2.在foreach循环前面添加以下行

    <?php $newFeature = array_unique($newFeature); ?>
    
    
    

    这样可能会解决你的问题。我仍然建议您使用第一个建议并调试代码。

    您在哪里定义了$newFeature???它是一个数组,可以,但它在哪里?另外,如果您使用的是索引$i,那么使用foreach有什么意义?除非$newFeature是一个维度数组(在本例中,如果它是维度数组,您甚至无法正确访问它),否则您打印的值是相同值的两倍,因为$feat与$newFeature[$i]相同,请尝试使用print\r($newFeature);并检查数组本身是否包含相同的值两次..newFeature是链接。。当你点击它时,你会看到数组,你可以尝试使用函数array_unique(),无论如何,如果数组是你发布的数组,就不需要这个函数了。foreach(数组_唯一($newFeature)为$feat)