duanou2526
2014-05-08 13:05 阅读 48
已采纳

用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 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);
    

    demo

    点赞 评论 复制链接分享
  • dongmibeng5885 dongmibeng5885 2014-05-08 13:09

    with a simple foreach

    <?php
    $search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
    $replace = array('A', 'a', 'S', 's', 'Ž', ž);
    foreach($data as $key => $value){
        $data[$key] = str_replace($search, $replace, $data[$key]);
    }
    ?>
    
    点赞 评论 复制链接分享
  • dougouqin0763 dougouqin0763 2014-05-08 13:13

    A foreach works well

        $data = array(
                    'word1'=>'Šššš',
                    'word2'=>'Zzzzžzzž',
                    'word3'=>'Āāāa'
                    );
    
        $search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
        $replace = array('A', 'a', 'S', 's', 'Ž', ž);      
    
        foreach($data as $k => $v) { // for each element in $data
            $data[$k] = str_replace($search, $replace, $v); // do replacement and assign back to the array.
        }
    
        print "<pre>"; print_r($data); print "</pre>"; // print formatted array
    
    点赞 评论 复制链接分享
  • douhuigang9550 douhuigang9550 2014-05-08 13:17

    Try this

    function splReplace($string)
    {
        $search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
        $replace = array('A', 'a', 'S', 's', 'Ž', ž);
        return str_replace($search, $replace, $string);
    }
    
    $data = array(
                    'word1'=>'Šššš',
                    'word2'=>'Zzzzžzzž',
                    'word3'=>'Āāāa'
                    );
    
    $data = array_map('splReplace',$data)   ;
    
    点赞 评论 复制链接分享
  • dp5657087 dp5657087 2014-05-08 13:23

    To answer your question you can use a foreach with a reference:

    $search  = array('Ā', 'ā', 'Š', 'š');
    $replace = array('A', 'a', 'S', 's');
    foreach ($data as &$word) {
        $word = str_replace($search, $replace, $word);
    }
    

    However, an easier way would to use a foreach with normalizer_normalize:

    foreach ($data as &$word) {
        $word = normalizer_normalize($word);
    }
    

    or using array_map:

    $data = array_map('normalizer_normalize', $data);
    

    Please note that normalizer_normalize was added to the core in PHP 5.3, but in most linux distributions it isn't active by default.

    You can also try out iconv():

    foreach ($data as &$word) {
        $word = iconv('UTF-8', 'ASCII//TRANSLIT', $word);
    }
    
    点赞 评论 复制链接分享
  • doujinge9648 doujinge9648 2014-05-08 13:38

    The third parameter, $subject for the str_replace function can be an array. So no need to loop through the values yourself. Tested on php 5.5, it'll keep your keys and values intact

    $search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
    $replace = array('A', 'a', 'S', 's', 'Z', 'z');
    
    $data = array(
                'word1'=>'Šššš',
                'word2'=>'Zzzzžzzž',
                'word3'=>'Āāāa'
    );
    
    $data = str_replace($search, $replace, $data);
    
    点赞 评论 复制链接分享

相关推荐