Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_Pdo_While Loop - Fatal编程技术网

在循环PHP中隐藏同名项

在循环PHP中隐藏同名项,php,loops,pdo,while-loop,Php,Loops,Pdo,While Loop,我只想在PHP循环中显示一次项。因此,如果在$item['name']中有多个相同的值,它应该只显示一次 这是我的循环: <?php while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) { ?> <li>

我只想在PHP循环中显示一次项。因此,如果在
$item['name']
中有多个相同的值,它应该只显示一次

这是我的循环:

                    <?php
                    while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) {
                        ?>
                        <li>
                            <i class="fa fa-archive"></i>
                            <h5><?php echo $item['name']; ?></h5>
                            <h3><?php echo $item['displayname']; ?></h3>
                            <a href="<?php echo $item['downloadurl']; ?>" class="btn btn-default pull-right">Download</a>
                            <div class="clearfix"></div>
                        </li>
                        <?php
                    }
                    ?>


  • 但我不知道如何隐藏其他同名物品。我在谷歌上也找不到关于这个的任何信息(或者我使用了错误的搜索词?)。希望有人能给我一个建议

    你是否试图做一些事情,比如如果我理解正确,我认为你正在尝试做与我相同的事情。

    其他人给了你很好的解决方案来解决你的问题,但这会让你在应用程序运行时花费更多的处理时间。
    $array=array();
    foreach ($item['name'] as $i) {
        if (!in_array($i, $array)) {
            $array[] = $i;
        }
    }
    print_r($array);
    
    您可以在sql select查询中使用DISTINCT关键字,这样您只需自动获取一次名称

    请参阅本教程:

    使用html代码。因此获得(高度)提高的代码可读性。当您以这种方式创建大量文件时,其优势将变得显而易见。 不过也有一些(罕见的)缺点,比如这里出现的缺点:为了避免将db代码与html代码混合,我不得不应用一个额外的数组迭代:用于构建
    $displaytems
    数组的迭代


    否则,如果您想使用您的方法,那么解决方案将是:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Demo</title>
    
            <!-- Css/Js resources -->
        </head>
        <body>
    
            <ul id="items">
                <?php
                // Produce your prepared statement $stmt here...
    
                // The list of unique item names.
                $itemNames = array();
    
                while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    $name = $item['name'];
    
                    if (!in_array($name, $itemNames)) { // If item name not already appended to the unique names list.
                        // Append item name to the unique names list.
                        $itemNames[] = $name;
    
                        $displayName = $item['displayname'];
                        $downloadUrl = $item['downloadurl'];
                        ?>
                        <li>
                            <i class="fa fa-archive"></i>
                            <h5>
                                <?php echo $name; ?>
                            </h5>
                            <h3>
                                <?php echo $displayName; ?>
                            </h3>
                            <a href="<?php echo $downloadUrl; ?>" class="btn btn-default pull-right">
                                Download
                            </a>
                            <div class="clearfix"></div>
                        </li>
                        <?php
                    }
                }
                ?>
            </ul>
    
        </body>
    </html>
    
    
    演示
    

    祝你好运。

    为什么你不能保留一个你已经循环过的数组,并且只有在以下情况下才能回音!在_array()中,我在许多关于_array的答案中看到,我建议使用:``如果(isset($u[$item['name']]){continue;}$u[$item['name']]]=1;``它将提高性能。你是说运行“辅助”吗在运行sql查询获取项目之前,sql查询只获取不同的名称?谢谢。是的,类似这样的。
    while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) {...}
    
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Demo</title>
    
            <!-- Css/Js resources -->
        </head>
        <body>
    
            <ul id="items">
                <?php
                // Produce your prepared statement $stmt here...
    
                // The list of unique item names.
                $itemNames = array();
    
                while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    $name = $item['name'];
    
                    if (!in_array($name, $itemNames)) { // If item name not already appended to the unique names list.
                        // Append item name to the unique names list.
                        $itemNames[] = $name;
    
                        $displayName = $item['displayname'];
                        $downloadUrl = $item['downloadurl'];
                        ?>
                        <li>
                            <i class="fa fa-archive"></i>
                            <h5>
                                <?php echo $name; ?>
                            </h5>
                            <h3>
                                <?php echo $displayName; ?>
                            </h3>
                            <a href="<?php echo $downloadUrl; ?>" class="btn btn-default pull-right">
                                Download
                            </a>
                            <div class="clearfix"></div>
                        </li>
                        <?php
                    }
                }
                ?>
            </ul>
    
        </body>
    </html>