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.

dfcode3
  • 809
  • 1
  • 9
  • 15
  • check out my answer. See below. If I'm correct, please credit me so I get points. Points motivate me to answer more questions. – Steve Nguyen 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. – Steve Nguyen 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()`](http://www.php.net/manual/en/function.strstr.php), but in the code you've posted, you used the function [`strtr()`](http://www.php.net/manual/en/function.strtr.php). They are similarly named, but different functions. Which did you mean? – Asaph Jun 24 '11 at 20:06

4 Answers4

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

Steve Nguyen
  • 5,854
  • 5
  • 21
  • 39
  • Sweet! That was it. Thanks! – dfcode3 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. – Steve Nguyen Jun 24 '11 at 18:32
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.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • Any way I can use preg_replace or pre_match without the loop? – dfcode3 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. – Steve Nguyen Jun 24 '11 at 18:30
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
)
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Good LORD man, do NOT use Globals. I don't want to bring back the days of spaghetti code. – Steve Nguyen 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. – Steve Nguyen Jun 24 '11 at 18:58
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);
?>
Asaph
  • 159,146
  • 25
  • 197
  • 199
  • One problem tho. It doesn't work. Do NOT use strstr() `http://codepad.org/CMnFwJrC` Proof that it doesn't work. – Steve Nguyen 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()`](http://www.php.net/manual/en/function.strstr.php). I used [`strtr()`](http://www.php.net/manual/en/function.strtr.php). 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. – Steve Nguyen Jun 24 '11 at 20:06