获得除了一个循环之外

hex*_*mal 0 php

我正在创建一个名为get_other_sites的函数.但我认为有一种比我现在做的更好或更快的方式.

function get_other_sites($curent_site){
    if ($curent_site == 'site1.com'){
        echo"
            <a href='site2.com'>site2.com</a>
            <a href='site3.com'>site3.com</a>
            <a href='site4.com'>site4.com</a>
            <a href='site5.com'>site5.com</a>
            ";
    }
    else if ($curent_site == 'site2.com'){
        echo"
            <a href='site1.com'>site1.com</a>
            <a href='site3.com'>site3.com</a>
            <a href='site4.com'>site4.com</a>
            <a href='site5.com'>site5.com</a>
            ";
    }
    else if ($curent_site == 'site3.com'){
        echo"
            <a href='site1.com'>site1.com</a>
            <a href='site2.com'>site2.com</a>
            <a href='site4.com'>site4.com</a>
            <a href='site5.com'>site5.com</a>
            ";
    }
}
Run Code Online (Sandbox Code Playgroud)

对于这个问题,我让它更容易阅读,但网站列表要长得多.有人知道一个更好的方法来做这个很少的文字吗?

提前致谢.

Han*_*nky 5

$current_site="def.com"; // testing
$sites=array("abc.com","def.com","ghi.com","jkl.com");
foreach($sites as $site){
     if($current_site!=$site)
       echo "<a href='$site'>$site</a>";
}
Run Code Online (Sandbox Code Playgroud)

就这么简单:P

小提琴