doubi8965 2016-07-15 02:41
浏览 46
已采纳

多维数组替换中的PHP效率

I have a situation where i need to loop through various XML documents and replace certain values with xPath. So far so good, but the approach i'm using just feels wrong (but it works, that's at least something).

What i did is i created a multidimensional array for each replacement type (in order to keep it manageable) with the replacements in it. Then i loop through these arrays and push the values to seperate arrays wich i use to replace the values with. Like this:

 $regions_original             =    Array();
 $regions_replaced             =    Array();

 $region_replacements = Array(
      "Wrong spelled area 1"   =>   "Area 1", 
      "Wrong spelled area 2"   =>   "Area 2", 
      "Wrong spelled area 3"   =>   "Area 3"
 );

 /*
 *   Split regions key/value pairs into two seperate arrays
 */
 foreach($region_replacements as $original => $replacement){
      array_push($regions_original, $original);
      array_push($regions_replaced, $replacement);
 }

 $terms                        = Array('Area 1','Area 1','Wrong spelled area 1','Area 2','Wrong spelled area 2');

 foreach($terms as $term){
      echo '<li>'.str_replace($regions_original, $regions_replaced, $term).'</li>';
 }

Looking at this makes me sick.. It just doesn't feel right. Also this approach also replaces occurrences within a sentence (when that's not what i want, see example below) so i thought of another approach by checking if the array key exists in the initial array and replace the value if it does. Like this:

 foreach($terms as $term){
      if(array_key_exists($term, $region_replacements)){
           echo '<li>'.$region_replacements[$term].'</li>';    
      }else{
           echo '<li>'.$term.'</li>';
      }
 }

This works, and even better than the first approach because with the first approach things like the following term are getting messed up:

 $terms = array('Sainte Foy');
 $replacements = array('Sainte Foy' => 'Domaine de Sainte Foy Tarantaise');
 $output = echo '<li>'.str_replace($regions_original, $regions_replaced, $term).'</li>';
 //Outputs: Domaine de Domaine de Sainte Foy Tarantaise Tarantaise

The thing with the last approach is that it is slower to execute. I've tested it with an XML file of about 500 elements and the difference is around 0.3sec in execution time (measured by comparing the microtime at the top and the bottom of the document). Because the source XML files can get a lot bigger i want the code to be efficient and managable as possible.

The second approach however 'feels' a lot better, although i can't stand the (apparantly) inefficiency. I hope that anyone can ether reassure me that one of these methods is fine or point me in a right direction to make it truly efficient. Would be great! :)

展开全部

  • 写回答

1条回答 默认 最新

  • douren2395 2016-07-15 04:27
    关注

    You can simply use your $region_replacements array as a lookup table, and if no result is found, echo the original term instead:

    $region_replacements = [
        "Wrong spelled area 1"   =>   "Area 1", 
        "Wrong spelled area 2"   =>   "Area 2", 
        "Wrong spelled area 3"   =>   "Area 3"
    ];
    
    $terms = ['Area 1','Area 1','Wrong spelled area 1','Area 2','Wrong spelled area 2'];
    
    foreach($terms as $term){
       echo '<li>';
       echo isset($region_replacements[$term]) ? $region_replacements[$term] : $term;
       echo '</li>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部