【问题标题】:php count array rows with same idphp计算具有相同ID的数组行
【发布时间】:2018-03-19 09:28:37
【问题描述】:

大家好,我有一个脚本循环将数据放入 CSV 文件的数组,我需要计算具有相同 ID 的行。

这是我的脚本,它循环数组并将其放入 csv 文件中以供导出。

public function fputToFile($file, $allexportfields, $object, $ae)
{
    if($allexportfields && $file && $object && $ae)
    {
        //one ready for export product
        $readyForExport = array();
        //put in correct sort order
        foreach ($allexportfields as $value)
        {
            $object = $this->processDecimalSettings($object, $ae, $value);
            $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);
        }
        //write into csv line by line
        fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
    }
}

我试过用:

$numero_riga = array_count_values($readyForExport);

$readyForExport['numero_riga'] = $numero_riga;

但是因为它是一个多维数组,所以在 csv 文件 meabe 中没有打印出任何正确的值,你可以在下面的文本和截图中看到 csv 导出:

ID 行号
198 阵列 199 阵列 200 阵列 200 阵列 201 阵列 201 阵列 201 阵列 201 阵列 202 阵列 202 阵列 203 阵列 203 阵列 203 阵列 204 数组 204 数组 204 数组 204 数组 204 数组 205阵列 205阵列 205阵列 206 阵列 207 阵列 207 阵列 208 阵列 209 阵列

csv export

结果必须在下面的文本和屏幕截图中是这样的,您可以看到一列计算具有相同 ID 的行。

ID 行号
176 1 177 1 177 2 178 1 178 2 179 1 179 2 180 1 181 1 181 2 182 1 182 2 183 1 184 1 184 2 185 1 185 2 186 1 186 2 186 3

correct result

提前致谢。

编辑

编辑了来自 scaisEdge 的 whit 建议,但现在 csv 导出以一种奇怪的方式运行。我把截图贴在这里csv strange behaviour

编辑

现在我在 scaisEdge 的帮助下使用此代码,我认为我们已经接近解决方案。

             $cnt_row = 0;
             $match_id = -1;
            //put in correct sort order
            foreach ($allexportfields as $value)
            {
                if ( $value['id_order'] == $match_id){
                   $cnt_row++;
                } else {
                   $cnt_row =1;
                   $match_id  = $value['id_order'];
                }
                //$value['num_row'] = $cnt_row;
                print_r($cnt_row);
                $object = $this->processDecimalSettings($object, $ae, $value);
                $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);

            }

            $readyForExport['num_row'] = $cnt_row;

我在此处粘贴实际结果的屏幕截图:partially correct result 您可以看到现在在正确的列中打印一些值,但始终打印“4”...

【问题讨论】:

  • 更新您的问题并添加一个简单的数据样本(作为文本,而不是图像)和预期结果
  • 感谢您对我的问题感兴趣。希望现在写得正确
  • 您想为每一行添加相对于 id 的序列的相对数量? ..你有一个foreach for manager的代码.?? .. 更新您的问题并添加管理结果的每个问题
  • 再次感谢我为上面的每个代码添加了
  • 是的,我需要在 csv 行中打印相对于 id 的序列数

标签: php arrays csv row counter


【解决方案1】:

试试这个:

<?php
class Yourclass {
    private $counter = null;

    public function fputToFile($file, $allexportfields, $object, $ae)
    {
            if($allexportfields && $file && $object && $ae)
            {
                    //one ready for export product
                    $readyForExport = array();

                    //put in correct sort order
                    foreach ($allexportfields as $value)
                    {
                            $object = $this->processDecimalSettings($object, $ae, $value);
                            $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);
                    }

                    $this->counter[$readyForExport['id_order']] = (!empty($this->counter[$readyForExport['id_order']])) ? ++$this->counter[$readyForExport['id_order']] : 1;
                    $readyForExport['orderLine'] = $this->counter[$readyForExport['id_order']];

                    fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
            }
    }
}

【讨论】:

  • 谢谢,但是我必须在使用 fputcsv 函数在 csv 文件中打印之前将行数据放入一个变量中,我们可以修改 foreach 行计数器以放入一个 $readyForExport['num_row'] 数组字段,该字段可以在 csv 列中打印结束?
  • 我需要在将另一个列打印到 csv 文件之前附加另一个列,因为有更多代码来处理 $readyForExport[$value] 数据。必须尽可能少地修改 fputcsv,所以我需要计算行号并将其放入打印到 fputcsv 的 $readyForExport[orderLine] 字段中。非常感谢
  • 需要这样的东西stackoverflow.com/questions/49359589/… Nigel Ren的那个
  • 对不起这个stackoverflow.com/questions/49367509/…来自 Nigel Ren 最后一个答案。我需要将您制作的 foreach 循环放在一个变量中,该变量将在 fputcsv 中打印。非常感谢
  • meabe 是这样的:$readtForExport['orderLine'] = $counts[$value['id_order']]['rows'];
【解决方案2】:

您应该检查 id_order 何时更改而不是数组中元素的数量

public function fputToFile($file, $allexportfields, $object, $ae)
{
    if($allexportfields && $file && $object && $ae)
    {
        //one ready for export product
        $readyForExport = array();
        //put in correct sort order
        $cnt_row = 0;
        $match_id = '';
        //cicle through the array
        foreach ($allexportfields as $value)
        {
            if ( $match_id == $value['id_order']){
               $cnt_row++;
            } else {
               $cnt_row =1;
               $match_id  = $value['id_order'];
            }
            $value['num_row'] = $cnt_row;
            $object = $this->processDecimalSettings($object, $ae, $value);
            $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);

        }
        //write into csv line by line
        fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
    }
}

【讨论】:

  • 我发现了一些小错误,缺少 2 个分号,没有什么很难纠正的 :-)
  • 回答更新 d.. 缺失;在 $value['num_row'] 之后
  • 现在它会导致 csv 格式错误的奇怪行为......我已经编辑了这样的代码: $value['num_row'] = (string)$cnt_row;由于强制低于 UTF-8
  • 我不明白您的评论..(字符串)转换已解决或您有问题??
  • 不幸的是不是......但我认为这是问题所在......我想粘贴 csv 的屏幕截图,但不知道如何将其放在评论中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 2013-08-05
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
相关资源
最近更新 更多