PHP计数具有相同ID的数组行 [英] php count array rows with same id

查看:159
本文介绍了PHP计数具有相同ID的数组行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello everybody I have a script that loops an array that put data in a CSV file, i need to count the rows with same ID.

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

this is my scritpt that loops the array and put it in a csv file for export.

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导出:

but it does not print any correct value in the csv file meabe because it is a multi dimensional array, you can see the csv export in the text and screenshot below:

ID row_number


198 Array
199 Array
200 Array
200 Array
201 Array
201 Array
201 Array
201 Array
202 Array
202 Array
203 Array
203 Array
203 Array
204 Array
204 Array
204 Array
204 Array
204 Array
205 Array
205 Array
205 Array
206 Array
207 Array
207 Array
208 Array
209 Array

csv导出

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

The result have to be like this in the text and screenshot below you can see a column that counts the rows with same ID.

ID row_number


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

正确的结果

谢谢.

编辑

编辑了scaisEdge的丝毫建议,但现在csv导出的行为很奇怪.我在此处粘贴屏幕截图 csv异常行为

Edited whit suggestions from scaisEdge but now the csv export acts in a strange way. I paste screenshot here csv strange behaviour

编辑

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

now I'm using this code whit the help of scaisEdge, i think we are close to the solution.

             $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;

我将实际结果的屏幕截图粘贴到此处:部分正确的结果看到现在正在正确的列中打印一些值,但始终会打印"4" ...

i paste the screenshot of the actual result here: partially correct result you can see that now is printing some values in the correct column but is prints " 4 " all the time...

推荐答案

尝试一下:

<?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);
            }
    }
}

这篇关于PHP计数具有相同ID的数组行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆