doubi8965 2016-07-15 10: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 12: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>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看