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条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动