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++;
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?