douba05167 2013-11-16 16:01
浏览 36
已采纳

PHP - 循环内的数组返回空

I am trying to add data to 2 different MySql tables through a CSV upload. I have been able to retrieve the information and upload part of the information on one table, but I am having issues sending data to the second table. I have come up with the following code:

$i = 0;
//Loop through the csv file
do {
    if ($data[0]) {

        //Store request information
        //Info for Request Table
        $requester_email = $data[0];
        $requester_name = $data[1];
        $client_name = $data[2];
        $client_country = $data[3];
        $opportunity_number = $data[4];
        $machine_quantity = $data[5];
        $severity = $data[6];

        //Store machine values
        //Info for Serial Numbers Table
        $serialType = array();
        $serialModel = array();

            $serialType[$i] = $data[7];
            $serialModel[$i] = $data[8];

    }
    $i++;
} while ($data = fgetcsv($handle,1000,",","'"));

I am having an issue with the array. I have only been able to store the serialType and serialModel from the last line in the csv file. For example, if the csv file has 3 lines of data:

( [0] => Empty [1] => Empty [2] => Last Value ok )

How come I am unable to store the other two values?

  • 写回答

1条回答 默认 最新

  • doupai1876 2013-11-16 16:04
    关注

    Just move

    $serialType = array();
    $serialModel = array();
    

    before do in your script. While it is still inside the do loop it is just a initialize variable in do - while scope each iteration and clean it up.

    $i = 0;
    
    $serialType = array();
    $serialModel = array();
    
    while ($data = fgetcsv($handle,1000,",","'")) {
    ...
        $serialType[$i] = $data[7];
        $serialModel[$i] = $data[8];
        $i++;
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序