php count array rows with same id

2019-09-19 19:59发布

问题:

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.

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

I've tried to use :

$numero_riga = array_count_values($readyForExport);

$readyForExport['numero_riga'] = $numero_riga;

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 export

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

correct result

Thanks in advance.

EDIT

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

EDIT

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;

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...

回答1:

Try this:

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


回答2:

you should check when the id_order change not the number of the elements in array

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