duanduanxi9441 2011-06-24 18:11
浏览 154
已采纳

strstr替换PHP数组中的多个变量

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条回答 默认 最新

  • doupo6967 2011-06-24 18:18
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!