大家好我有一个脚本循环将数据放入CSV文件的数组,我需要计算具有相同ID的行。
这是我的scritpt循环数组并将其放在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文件中打印任何正确的值因为它是一个多维数组,你可以在下面的文本和截图中看到csv导出:
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
结果必须在下面的文本和屏幕截图中看到,您可以看到一个列,它会计算具有相同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编辑了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你可以看到现在正在正确的列中打印一些值,但是打印了#34; 4"一直......
答案 0 :(得分:0)
你应该检查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);
}
}
答案 1 :(得分:0)
试试这个:
<?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);
}
}
}