dougan7523 2015-09-16 09:48
浏览 25
已采纳

PHP在数组中取消设置数据

I have the following line which reads a csv and turns each row into an Array

$reader = new CSV\CSVReader('somecsv.csv');

So if I then do

while ($row = $reader->getRow()) {
    print "<pre>";
    print_r($row);
    print "</pre>";
}

It outputs data like so

Array
(
    [Column1] => some
    [Column2] => data
    [Column3] => hi
    [Column4] => wow
    [Column5] => help
)
Array ...

Say I wanted to remove column1, inside the while loop I can place

unset($row['column1']);

And this will remove column1 from the output. However, if I place a function in my $reader class like so

public function unsetColumns($row)
{
    unset($row['column1']);
}

And I change my while loop to the following

while ($row = $reader->getRow()) {
    $reader->unsetColumns($row);  //this does not work
    unset($row['column1']);  //this works
    print "<pre>";
    print_r($row);
    print "</pre>";
}

Then the function call does not remove the column, but the unset does. I dont have both in there at the same time, just put them both there so you can see what works and what does not.

Why would this be?

Thanks

  • 写回答

3条回答 默认 最新

  • duanjia7607 2015-09-16 09:53
    关注

    You can pass your array by reference read here more on Passing by Reference

    public function unsetColumns(&$row)
    {
        unset($row['column1']);
    }
    
    while ($row = $reader->getRow()) {
        $reader->unsetColumns($row);  //this does not work
        unset($row['column1']);  //this works
        print "<pre>";
        print_r($row);
        print "</pre>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题