Simplifying an associative array() Would it be better to convert the array in the following code to

$langs = array("en", "de", "fr");


and then reusing the values for both $folder and $flag? If so, how then would my foreach (or maybe a while?) loop be written?

&lt;?php
    $langs = array(
        "en/" =&gt; "en.png",
        "de/" =&gt; "de.png",
        "fr/" =&gt; "fr.png"
    );
    $self    = $_SERVER['REQUEST_URI'];
    $pattern = "{^.*/}i";
    $links   = array();
    foreach ($langs as $folder =&gt; $flag) {
        $url  = preg_replace($pattern, "$folder", $self);
        $link = "&lt;li&gt;&lt;a href=\"../$url\"&gt;&lt;img src=\"../img/$flag\"&gt;&lt;/a&gt;&lt;/li&gt;";
        array_push($links, $link);
    }
    echo implode($links) . "\n";
?&gt;


I'm trying to "fool proof" the code, by effectively limiting the folder structures and file names that can be used, as I create a basic template of files for quick rolling out of our websites. If you can see any other improvements, that would be much appreciated.
You can definitely simplify this, as the only part of each element of $langs that changes is the two-letter language code. Something like this would work:

&lt;?php
$langs = array(
    "en",
    "de",
    "fr"
);
$self    = $_SERVER['REQUEST_URI'];
$pattern = "{^.*/}i";
$links   = array();
foreach ($langs as $code) {
    $url  = preg_replace($pattern, "$code/", $self);
    $link = "&lt;li&gt;&lt;a href=\"../$url\"&gt;&lt;img src=\"../img/$code.png\"&gt;&lt;/a&gt;&lt;/li&gt;";
    array_push($links, $link);
}
echo implode($links) . "\n";
?&gt;


You just use a non-associative array and append / or .png where needed; much cleaner.