2

I'm working on a list of products that are written in multiple languages. I have an array for each product that displays it's languages like this:

Array ( [0] => DA [1] => DE [2] => EN [3] => ES [4] => FI [5] => FR [6] => IT [7] => JA [8] => KO [9] => NL [10] => NO [11] => PL [12] => PT [13] => RU [14] => SV [15] => ZH )

I need to replace these individual codes with their language names (so EN => English). I have the following code, and it works fine with regular strings, but I can't get it to work with this array. Any thoughts?

    $trans = array(
        "EN" => "English", 
        "ZH" => "Chinese", 
        "DA" => "Danish",
        "NL" => "Dutch", 
        "FI" => "Finnish", 
        "FR" => "French",
        "DE" => "German", 
        "IT" => "Italian", 
        "JA" => "Japanese",
        "KO" => "Korean", 
        "NO" => "Norwegian", 
        "PL" => "Polish",
        "PT" => "Portuguese", 
        "RU" => "Russian", 
        "ES" => "Spanish",
        "SV" => "Swedish", 
    );

    echo strtr($langcodes, $trans);

$langcodes holds the array values.

4
  • check out my answer. See below. If I'm correct, please credit me so I get points. Points motivate me to answer more questions. Commented Jun 24, 2011 at 18:22
  • @FinalForm: How about a solution that actually uses the strtr() function? Commented Jun 24, 2011 at 18:24
  • @Asaph not thinking indepth about strstr(), but just going over it quickly in the php.net. My "gut" feeling tells me this will produce a less explicit solution that'll be more difficult to read, code-wise, and possibly produce a more complex solution. Commented Jun 24, 2011 at 18:28
  • I realize now that this question has caused some confusion among the answerers. In your title, you mentioned the php function strstr(), but in the code you've posted, you used the function strtr(). They are similarly named, but different functions. Which did you mean? Commented Jun 24, 2011 at 20:06

4 Answers 4

2

Proof that it works: http://codepad.org/PR5pPqcX

@David check out my answer. See below. If I'm correct, please credit me so I get points. Points motivate me to answer more questions.

$language_codes = array(0 => 'DA', 1 => 'DE', 2 => 'EN', 3 => 'ES', 4 => 'FI', 5 => 'FR', 6 => 'IT', 7 => 'JA', 8 => 'KO', 9 => 'NL', 10 => 'NO', 11 => 'PL', 12 => PT, 13 => 'RU', 14 => 'SV', 15 => 'ZH' );

$trans = array(
    "EN" => "English", 
    "ZH" => "Chinese", 
    "DA" => "Danish",
    "NL" => "Dutch", 
    "FI" => "Finnish", 
    "FR" => "French",
    "DE" => "German", 
    "IT" => "Italian", 
    "JA" => "Japanese",
    "KO" => "Korean", 
    "NO" => "Norwegian", 
    "PL" => "Polish",
    "PT" => "Portuguese", 
    "RU" => "Russian", 
    "ES" => "Spanish",
    "SV" => "Swedish", 
);


foreach ($language_codes as $key => $code)
    if (!empty($trans[$code]))
        $language_codes[$key] = $trans[$code];    

var_dump($language_codes);

Proof that it works: http://codepad.org/PR5pPqcX

Sign up to request clarification or add additional context in comments.

2 Comments

Sweet! That was it. Thanks!
@David Don't use strstr() for this particular problem. That will just add unneeded overhead. All that is required is an assignment to the variable, not running variables through another function for no good reason.
1

I think you have to loop through $langcodes and call strtr() for each code. According to the PHP manual, the first parameter has to be a string, not an array of strings.

3 Comments

Any way I can use preg_replace or pre_match without the loop?
@David, preg_replace can be run on an array of subjects and they will all be replaced. As long as you don't have any special characters in your language codes, it should work. But why not just do the loop and keep your code easier to read?
@David Why even use strstr() if all that is required is an assignment? I don't agree with strstr(), this will add unneeded overhead.
1

How about using array_map function like this:

function mapLang($l) {
   global $trans;
   return $trans[$l];
}
$langcodes = array_map("mapLang", $langcodes);
print_r($langcodes);

OUTPUT

Array
(
    [0] => Danish
    [1] => German
    [2] => English
    [3] => Spanish
    [4] => Finnish
    [5] => French
    [6] => Italian
    [7] => Japanese
    [8] => Korean
    [9] => Dutch
    [10] => Norwegian
    [11] => Polish
    [12] => Portuguese
    [13] => Russian
    [14] => Swedish
    [15] => Chinese
)

3 Comments

Good LORD man, do NOT use Globals. I don't want to bring back the days of spaghetti code.
@FinalForm: It depends how and where it is used. Here it is used for a lookup into a key based Array and IMO it is a valid case to avoid some extra (spaghetti) code and more importantly this answer avoids LOOPING of those arrays which was one the requirement as per the OP's comments.
k, I looked over your code again. I actually prefer your answer over the one I gave. It's more clever and re-usable.
0

The PHP docs for strtr() make no mention of array support for argument #1. Arrays are only supported for argument #2. This simply will not work. You'll have to roll your own loop. Here is how to do it:

<?php
$languages = array('DA', 'DE', 'EN', 'ES', 'FI', 'FR', 'IT', 'JA', 'KO', 'NL', 'NO', 'PL', 'PT', 'RU', 'SV', 'ZH');
$trans = array(
        'EN' => 'English', 
        'ZH' => 'Chinese', 
        'DA' => 'Danish',
        'NL' => 'Dutch', 
        'FI' => 'Finnish', 
        'FR' => 'French',
        'DE' => 'German', 
        'IT' => 'Italian', 
        'JA' => 'Japanese',
        'KO' => 'Korean', 
        'NO' => 'Norwegian', 
        'PL' => 'Polish',
        'PT' => 'Portuguese', 
        'RU' => 'Russian', 
        'ES' => 'Spanish',
        'SV' => 'Swedish', 
    );
foreach($languages as &$language) {
    $language = strtr($language, $trans);
}
print_r($languages);
?>

3 Comments

One problem tho. It doesn't work. Do NOT use strstr() http://codepad.org/CMnFwJrC Proof that it doesn't work.
@FinalForm: It does work. I tested it. Your codepad example is not a faithful representation of my answer. Your codepad example uses strstr(). I used strtr(). They are similarly named functions. See the difference?
My mistake. Ultimately, I feel @anubhava solution is the best. His answer is re-usable. Ours is not.

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.