dqsot35145 2013-05-29 20:30
浏览 26
已采纳

打开两个CSV文件,比较并将它们添加到数组中

I am trying to take info from two different two CSV files and add them to an array. What I do basically it's to open the first file, take it's content into an array as string. Then here it comes the tricky part. Both files have an ID field in common, so whenever the ID matches it has to be taken into the array.

I've tried to do this in two different ways, opening one file and within this opening the other, compare and save to the array. The other way was getting all the info from both files to two separe arrays and then find the match and get them to a third array.

Here's the code:

$handle0 = \fopen("/Data/mountain1.csv", "r");

if ($handle0) {
    $line0 = 0;
    while (($buffer0 = fgets($handle0, 4000)) !== false) {
        if ($line0 > 0){
            $mountainArray = str_getcsv($buffer0, ",");                 
            $obj = array();
            $obj["id"] = $mountainArray[2];
            $obj["name"] = $mountainArray[0];
            $obj["country"] = $mountainArray[1];

            $handle1 = fopen("/Data/mountain1.csv", "r");
            if ($handle1) {
                $line1 = 0;
                while (($buffer1 = fgets($handle1, 4000)) !== false) {
                    if ($line1 > 0) {
                        $latlonArray = str_getcsv($buffer1, ",");
                        $content = array();
                        $content["id"] = $latlonArray[1];
                        if ((int)$content["id"] == (int)$obj["id"]) {
                            $obj["latitude"] = $latlonArray[7];
                            $obj["longitude"] = $latlonArray[8];
                        }
                    $line1++;
                    }
                }
                fclose($handle1);
            }

            $mountain[] = $obj;
        }
        $line0++;
    }
    fclose($handle0);
}

This code just loops and does nothing

if ($handle0) {
    while (($buffer0 = fgets($handle0, 4000)) !== false) {
        $mountainArray = str_getcsv($buffer0, ",");
        $content0 = array();
        $content0["id"] = $mountainArray[2];
        $content0["name"] = $mountainArray[0];
        $content0["country"] = $mountainArray[1];

        $mountain[] = $content0;
    }
    fclose($handle0);
}

if ($handle1) {
    while (($buffer1 = fgets($handle1, 4000)) !== false) {
        $latlonArray = str_getcsv($handle1, ",");
        $content1 = array();
        $content1["id"] = $latlonArray[1];
        $content1["latitude"] = $latlonArray[7];
        $content1["longitude"] = $latlonArray[8];

        $latlon[] = $content1;
    }
    fclose($handle1);
}

foreach ($mountain as $row0) {
    $obj = array();
    $obj["id"] = $row0["productUid"];
    $obj["name"] = $row0["name"];
    $obj["country"] = $row0["address"];

    foreach ($latlon as $row1) {
        if((int)$row1["id"] == (int)$row0["id"]) {
            $obj["latitude"] = $row1["latitude"];
            $obj["longitude"] = $row1["longitude"];
        }
    }

    $mountains[] = $obj;
}

and this one just returns null to me...

  • 写回答

3条回答 默认 最新

  • dongzhan2029 2013-05-29 21:25
    关注

    From your code I assumed that:

    • For the CSV file containing mountains data: id is at position 2, name at 0 and country at 1.
    • For the CSV file with coordinates: id in 1, latitude in 7 and longitude in 8.

    I decided to give you a more thorough code snippet that'll work for any number of CSV files, you just have to add them to the $csvFiles array and use the file name as key and file type as value.

    <?php
    $result   = array();
    $csvFiles = array(
        'mountains.csv'   => 'Mountain',
        'coordinates.csv' => 'Coordinate'
    );
    
    foreach ($csvFiles as $csvFile => $type) {
        if ($handle = fopen($csvFile, 'r')) {
            $lineNumber = 0;
    
            while ($data = fgetcsv($handle, 128, ',')) {
                if (!$lineNumber) {
                    $lineNumber++;
                    continue;
                }
    
                switch ($type) {
                    // Store the record in the result array
                    case 'Mountain':
                        $record = array(
                            'id'      => $data[2],
                            'name'    => $data[0],
                            'country' => $data[1]
                        );
    
                        $id          = $record['id'];
                        $result[$id] = $record;
                        break;
    
                    // Add longitude and latitude to the record
                    // if already in the result array
                    case 'Coordinate':
                        $record = array(
                            'id'        => $data[1],
                            'latitude'  => $data[7],
                            'longitude' => $data[8]
                        );
    
                        $id = $record['id'];
                        if (!empty($result[$id])) {
                            $result[$id] = array_merge($result[$id], $record);
                        }
                        break;
                }
            }
        }
    }
    
    print_r($result);
    

    With the following files:

    mountains.csv

    # CSV headers
    aaa, USA, 1
    aab, Canada, 2
    aac, USA, 3
    bbb, Portugal, 4
    ccc, Germany, 5
    

    coordinates.csv

    # CSV headers
    asd, 1, asd, asd, asd, asd, asd, 10.00, 20.00
    asd, 2, asd, asd, asd, asd, asd, 1.00, 2.00
    asd, 4, asd, asd, asd, asd, asd, 5.00, 10.00
    asd, 3, asd, asd, asd, asd, asd, 2.00, 4.00
    asd, 5, asd, asd, asd, asd, asd, 100.00, 200.00
    

    the output will be:

    Array
    (
        [ 1] => Array
            (
                [id] =>  1
                [name] => aaa
                [country] =>  USA
                [latitude] =>  10.00
                [longitude] =>  20.00
            )
    
        [ 2] => Array
            (
                [id] =>  2
                [name] => aab
                [country] =>  Canada
                [latitude] =>  1.00
                [longitude] =>  2.00
            )
    
        [ 3] => Array
            (
                [id] =>  3
                [name] => aac
                [country] =>  USA
                [latitude] =>  2.00
                [longitude] =>  4.00
            )
    
        [ 4] => Array
            (
                [id] =>  4
                [name] => bbb
                [country] =>  Portugal
                [latitude] =>  5.00
                [longitude] =>  10.00
            )
    
        [ 5] => Array
            (
                [id] =>  5
                [name] => ccc
                [country] =>  Germany
                [latitude] =>  100.00
                [longitude] =>  200.00
            )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试