Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
check out my answer. See below. If I'm correct, please credit me so I get points. Points motivate me to answer more questions. – FinalForm Jun 24 '11 at 18:22
@FinalForm: How about a solution that actually uses the strtr() function? – Asaph Jun 24 '11 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. – FinalForm Jun 24 '11 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? – Asaph Jun 24 '11 at 20:06

4 Answers

up vote 2 down vote accepted

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

share|improve this answer
Sweet! That was it. Thanks! – David Jun 24 '11 at 18:29
@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. – FinalForm Jun 24 '11 at 18:32

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.

share|improve this answer
Any way I can use preg_replace or pre_match without the loop? – David Jun 24 '11 at 18:25
@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? – Don Kirkby Jun 24 '11 at 18:28
@David Why even use strstr() if all that is required is an assignment? I don't agree with strstr(), this will add unneeded overhead. – FinalForm Jun 24 '11 at 18:30

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
)
share|improve this answer
Good LORD man, do NOT use Globals. I don't want to bring back the days of spaghetti code. – FinalForm Jun 24 '11 at 18:49
@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. – anubhava Jun 24 '11 at 18:55
k, I looked over your code again. I actually prefer your answer over the one I gave. It's more clever and re-usable. – FinalForm Jun 24 '11 at 18:58

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);
?>
share|improve this answer
One problem tho. It doesn't work. Do NOT use strstr() http://codepad.org/CMnFwJrC Proof that it doesn't work. – FinalForm Jun 24 '11 at 18:47
@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? – Asaph Jun 24 '11 at 20:03
1  
My mistake. Ultimately, I feel @anubhava solution is the best. His answer is re-usable. Ours is not. – FinalForm Jun 24 '11 at 20:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.