duanou2526 2014-05-08 13:05
浏览 109
已采纳

用str_replace函数替换数组中的数据

i have an array named $data

$data = array(
                    'word1'=>'Šššš',
                    'word2'=>'Zzzzžzzž',
                    'word3'=>'Āāāa'
                    );

what i want to do is to create new array with the same keys but with different values, so my new array would look like this:

$newdata = array(
                    'word1'=>'Ssss',
                    'word2'=>'Zzzzzzzz',
                    'word3'=>'Aaaa'
                    ); 

all i want to do is to replace letters with diacritical mark to normal letter, i have searched for php functions and i have understood that i could use str_replace function like this:

$search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
$replace = array('A', 'a', 'S', 's', 'Ž', ž);
str_replace($search, $replace, $data);

but how can i loop through array checking and replacing values without touching keys? or i cannot do that?(all data have to be be checked)

  • 写回答

6条回答 默认 最新

  • dou72260 2014-05-08 13:10
    关注

    try like this:

     $data = array(
        'word1'=>'Šššš',
        'word2'=>'Zzzzžzzž',
        'word3'=>'Āāāa'
    );
    
    $search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
    $replace = array('A', 'a', 'S', 's', 'Ž', 'ž');
    $data2 = array();
    foreach($data as $k =>$v){
        $data2[$k] = str_replace($search, $replace, $data[$k]);
    }
    print_r($data2);
    

    you can change the line inside foreach by this:

    $data2[$k] = str_replace($search, $replace, $v);
    

    <kbd>demo</kbd>

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

报告相同问题?

悬赏问题

  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?