繁体   English   中英

为什么不取消从其中删除index.php的设置

[英]Why isn't unset removing index.php from it's

为什么不取消设置从我回显的结果中删除index.php?

$files = array(
    '0' => 'bob.php',
    '1' => 'index.php',
    '2' => 'fred.php'
);
foreach ($files as $key => &$file) {
    if(in_array($file, array('index.php'))) {
        echo 'test condition<br />'; // Yes, this condition is met
        unset($files[$key]);
    }
    echo '<a href="'.$file.'">'.$file.'</a><br />'."\n"; 
}

为此,我实际上遵循了这个stackoverflow问题的答案。

由于$file已经设置为index.php因此它仍然会回显。 密钥实际上是未设置的,您可以通过在循环中使用continue来修复代码:

<?php

$files = array("home.php","index.php","example.php");
    foreach ($files as $key => &$file) {
        if(in_array($file, array('index.php'))) {
            unset($files[$key]);
            continue;
        }
        echo '<a href="'.$file.'">'.$file.'</a><br />'."\n"; 
    }
?>

结果:

<a href="home.php">home.php</a><br />
<a href="example.php">example.php</a><br />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM