1

I have the following PHP code that replaces folder names in the URL structure, for easy switching of languages whilst staying on the current page:

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

However, I've realised that with a simple array reading $langs = array("en", "de", "fr"); I could possibly save a few bytes and simplify the bottom-half of the code, reusing the variables for the folder and for the image? Would this be a better way of doing things? And how?

This is what I have so far, but the $langs['$x'] seems wrong? (In fact, I get a syntax error on my loop conditions!):

$langs = array("en", "de", "fr");
$n = count($langs); // 3
while ($x = 0; $x < $n; $x++ ) {
    $folder = $langs['$x'] . "/";
    $flags = $langs['$x'] . ".png";
    echo $folder . " " . $flags; // test
}
6
  • 1
    Why does "saving a few bytes" matter? Commented Feb 6, 2014 at 14:54
  • Not only am I trying to get my code as small as possible, I'm trying to build a template system for our websites that is as fool-proof as possible. By "forcing" people to use particular folder structures/image names, it will mean that rolling out new sites should be much simpler, with far fewer lines of code being touched. Commented Feb 6, 2014 at 14:56
  • @MatthewPeckham, why not use an existing templating system? There are lots to choose from. Commented Feb 6, 2014 at 14:58
  • Unfortunately, we cannot redesign the sites we're working with. All I can do is improve and "automate" existing code; tidy up CSS, move common terms to variables and then to includes, translations to .ini files, etc. Commented Feb 6, 2014 at 15:01
  • Consider asking this on codereview.stackexchange.com. I can make some other suggestions, but they aren't on topic for this question. Commented Feb 6, 2014 at 15:04

2 Answers 2

2

You can convert an associative array to a numerically-indexed array with array_values:

$array = array('a' => 'A', 'b' => 'B', 'c' => 'C');

// array(0 => 'A', 1 => 'B', 2 => 'C');
$new_array = array_values($array);
Sign up to request clarification or add additional context in comments.

Comments

0

I think it is because you are using the wrong quotes, double quotes are required for variable substitution

$langs = array("en", "de", "fr");
$n = count($langs); // 3
while ($x = 0; $x < $n; $x++ ) {
    $folder = $langs["$x"] . "/";
    $flags = $langs["$x"] . ".png";
    echo $folder . " " . $flags; // test
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.