douzheren3349 2017-10-25 16:42
浏览 16
已采纳

PHP中的多维数组由mysql查询创建

I have a base table in a MySQL database that has 200,000 records.

The structure of this table is as follows:

    CREATE TABLE `npanxxmaster` (
  `npanxx` varchar(6) NOT NULL DEFAULT '',
  `npa` varchar(3) DEFAULT NULL,
  `ocn_lata` varchar(7) DEFAULT NULL,
  `lata` varchar(3) DEFAULT NULL,
  `st` varchar(2) DEFAULT NULL,
  `canada` varchar(10) DEFAULT NULL,
  `ext` varchar(10) DEFAULT NULL,
  `er` double DEFAULT NULL,
  `ra` double DEFAULT NULL,
  `un` double DEFAULT NULL
)

I'm storing this table into an array with the key being the npanxx value.

I have a flat csv file that has the following structure:

npanxx(or npa only), ER, RA , UN

For a little more information npanxx is a 6 digit number and npa will always be a 3 digit number (from the npanxx).

Now this flat file, in the first column can have either npanxx (6 digits) or npa (3 digits).

What I need to do is read in each line of the csv file (which I can already do) and I store that into an array with the following code:

if (($handle = fopen($fileName, "r")) !== FALSE) {
    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE){

Now here comes the tricky part. What I need to do is go through each of the $row (lines in the csv file) and find a match in the original $npanxxmaster array that I made from the MySQL table. The problem I have is the flat file can contain in its first column either npanxx or npa. And what I need to do with those are match them with the matching npanxx or npa in the $npanxxmaster array with any $row that has an npanxx taking precident over one that only has an npa.

For example:

If given in the flat csv file the following $rows:

201,.002,.002,.002
201200,.001,.001,.001

All entries in the $npanxxmaster array with an npa of 201 would get the .002,.002,.002 OTHER THAN 201200 which would get .001,.001,.001

I have no been able to achieve this at all. I can provide more code of what I have tried or more explanation if needed because i'm assuming I butchered that explanation.

Thank you all in advance for all the help!

  • 写回答

1条回答 默认 最新

  • duanli8391 2017-10-25 19:22
    关注

    When processing the query, make two arrays. One that maps NPANXX to rows, and another that maps NPA to an array of NPANXX.

    $npanxx_array = array();
    $npa_array = array();
    while ($row = $stmt->fetch()) {
        $npanxx = $row['npanxx'];
        $npa = $row['npa'];
        $npanxx_array[$npanxx] = $row;
        if (!isset($npa_array[$npa])) {
            $npa_array[$npa] = array();
        }
        $npa_array[$npa][] = $npanxx;
    }
    

    Then when you're processing the CSV, you can look up the appropriate field.

    while ($row = fgetcsv($handle)) {
        if (strlen($row[0]) == 6) {
            $npanxx_to_update = array($row[0]);
        } else {
            $npanxx_to_update = $npa_array[$row[0]];
        }
        foreach ($npaxx_to_update as $npanxx) {
            // update $npanxx_array[$npanxx]
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line