比较2个csv文件与PHP并返回更改的行号

时间:2014-10-17 03:30:35

标签: php csv

我的脚本比较了2个csv文件并将更改后的行ID更改为数组:

 //Function for comparing two files
 function row_compare($a, $b)
 {
 if ($a === $b)
 {
 return 0;
 }
 return (implode("",$a) < implode("",$b) ) ? -1 : 1;
 }

 //Set previous csv file
 $file1 = new SplFileObject("file1.csv");
 $file1->setFlags(SplFileObject::READ_CSV);

 //Set new csv file
 $file2 = new SplFileObject("file2.csv");
 $file2->setFlags(SplFileObject::READ_CSV);

 foreach ($file1 as $row)
 {
 $csv_1[] = $row;
 }

 foreach ($file2 as $row)
 {
 $csv_2[] = $row;
 }

 //Check differences
 $all_unique_rows = array_udiff($csv_1, $csv_2, 'row_compare');

 //Get rows id which have difference
 foreach($all_unique_rows as $key=>$unique_row)
 {
 foreach($unique_row as $element)
 {
 $rowwwithdiffer[] = array($key);
 }
 }

工作正常!但是我想要实现的 - 而不是获得1,2,3 - 使用1 - 3,它是 - 如果更改的行是连续的,则使用间隔而不是每个更改的行ID ...

我怎么能实现它? ; - )

TNX!

1 个答案:

答案 0 :(得分:0)

你必须使用id's运行数组,并检查它们是否是连续的,并相应地处理它们。

#include <iostream>
#include <string>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/graph/graph_utility.hpp>

struct Vertex
{
    bool isLeaf;
    std::string taxon;
};


typedef boost::property<boost::edge_weight_t, double> Edge;
typedef boost::adjacency_list<boost::vecS,boost::vecS, boost::undirectedS, Vertex, Edge> Graph;

int main()
{
    Graph g;

    boost::property_map<Graph, boost::edge_weight_t>::type weight = get(boost::edge_weight,g);  

    boost::dynamic_properties dp;
    dp.property("node_id", get(boost::vertex_index, g));
    dp.property("isLeaf", get(&Vertex::isLeaf, g));
    dp.property("time", weight);

    std::string blah = "Hello";

    std::ifstream dot("test.dot");

    boost::read_graphviz(dot,g,dp);

    g[1].taxon = blah;  
}