doubomudichen0832 2016-11-23 07:58
浏览 26

用随机启动php删除重复行

I have a file containing data something like this:

randomString-data1
randomString-data2
randomString-data4
randomString-data2
randomString-data2
randomString-data3
randomString-data4

The output I want:

randomString-data1
randomString-data2
randomString-data4
randomString-data3

The length of random string is between 20 to 25 characters. The randomString is not important. The data after it is of relevance only. What could be the best way to remove such duplicates?

All I can think of is the brute force way to explode each line at "-" and compare with all others. But is there a better way?

Note: The random strings are important. But it is okay if any one of the randomString remains from the duplicate entries.

  • 写回答

3条回答 默认 最新

  • dongque1958 2016-11-23 08:03
    关注

    firstly explode them by ' ' then by '-'

    $arr = explode('
    ', $str);
    
    $data = array();
    foreach($arr as $val)
    {
       $temp = explode("-", $val);
       $data[$temp[1]] = $temp[0];
    }
    
    $str = "";
    foreach($data as $k => $v)
    {
      $str .= $v . "-" . $k;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测