duanguai2781 2018-07-17 04:20
浏览 60

将CSV数据组合到php数组中,带来一些无关的结果

This code works almost perfectly. I have two csv files which I am building into arrays and then merging the arrays to contain the relevant information. The first contains part numbers, short description, and spc(category). The second contains Part numbers and amount available.

The idea is to match merge this information based on the part number and the sort the information and display it based on spc(category).

After testing out the readouts of each array, the problem seems to occur at STEP 4 and further compounds at STEP 5.

There are about 70,000 records. The majority of these are merged and displayed properly, they look like this:

[#10-CH-L/W] => Array
        (
            [0] => 362.00
            [1] => HIGH COLLAR LOCK WASHER
            [2] => NUTBLT
        )

but at STEP 4, there are descriptions that get entered into their own array like this: (these should be in the [1] spot for an associated part)

[5872] => LINCOLN ADAPTOR 90-DEG 1/8 X 1/8
    [5873] => MILW CYL H SERIES SPECIAL 8 X 16
    [5874] => MILW CYL NEENAH # 55-40-6080-7
    [5875] => MILW CYL L&S #DS001268 2 X 10
    [5876] => CONTINENTAL PART SPOOL F1 V*50M 

And at Step 5, further arrays are created from spcs like this: (these should be in the [2] spot for an associated part)

 [11360] => NRGFIT
    [11361] => NRGFIT
    [11362] => NRGFIT
    [11363] => LINLUB
    [11364] => CHDPRT

From looking at the data in the csvs, the data seems to be consistant. The records where the problem occurs DO have part numbers that exist on BOTH documents. Is this an issue with the array_merge_recursive function? Do I need to look harder at the data? This is so close to being perfect!

       <?php    
               $file1  = fopen('inventory/MasterInfoRows.csv', 'r');
                $file2 = fopen('inventory/ItemLocationRows.csv', 'r');

                $line = array();
                $work = array();


                //create arrays for part number, description, and spc from Masterinforows.csv

                while (($line = fgetcsv($file1)) !== FALSE){

                   list($part, $desc, $spc) = $line;

                    $test1[] = $part;
                    $test2[] = $desc;
                    $test3[] = $spc;



                }               

               //create arrays for part number and available amount from itemlocationrows.csv

                while (($work= fgetcsv($file2, 1000)) !== FALSE) {

                    list($match, , , , , , , , $available) = $work;

                        $parts[] = $match;
                        $amounts[] = $available;


                    } 


                    // STEP 1 combine ItemLocationRows CSV part numbers with associated amounts 
                    $results = array_combine($parts, $amounts);

                    // STEP 2 combine MasterInfoRows part numbers with short descriptions
                    $master = array_combine($test1, $test2);

                    // STEP 3 combine MasterInfoRows part numbers with SPC
                    $master2 = array_combine($test1, $test3);

                    // STEP 4 combine itemlocation row part numbers and amount with Masterinfo Row part numbers short descriptions (about 6,000/70,000 records do not merge properly)
                    $alldata = array_merge_recursive($results, $master);


                    // STEP 5 combine itemlocationrow part numbers, amounts, and spc with MasterInfoRows part numbers and short descriptions (additional 5,000 extraneous spc values in array)
                    $success = array_merge_recursive($alldata, $master2);               



        //search through final array for records containing a specific spc in the array and return associated data

        function searchForId($id, $array) {
                    $keyarray = array();
                       foreach ($array as $key => $val) {
                            if (isset($val[2]) && $val[2] === $id) {
                                 array_push( $keyarray, $key);
                                 echo "<tr><td>$key</td><td>$val[1]</td><td>$val[0]</td></tr>";
                                }

                           }
                            return $keyarray;

               }    





        ?>

edit A possible clue: itemlocationrows contains: 75,300 Records, whereas masterlocationrows contains 70,144

  • 写回答

1条回答 默认 最新

  • douxian1895 2018-07-24 02:31
    关注

    Ok so I have narrowed down the problem to the fact that array_merge_recursive overwrites numeric keys. So the incorrectly merged records do in fact have numeric keys. Still trying to figure out a work-around that gives me the desired output.

    评论

报告相同问题?

悬赏问题

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