Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过php对csv数据进行排序_Php_Html_Csv - Fatal编程技术网

通过php对csv数据进行排序

通过php对csv数据进行排序,php,html,csv,Php,Html,Csv,我的服务器上有一个.csv文件,如下所示: C:/Users/Server/Pictures/P1040805.JPG,0.163533 C:/Users/Server/Pictures/P1040808.JPG,0.170159 C:/Users/Server/Pictures/P1040819.JPG,0.885534 C:/Users/Server/Pictures/P1040825.JPG,0.11393 C:/Users/Server/Pictures/P1040826.JPG,0.0

我的服务器上有一个.csv文件,如下所示:

C:/Users/Server/Pictures/P1040805.JPG,0.163533
C:/Users/Server/Pictures/P1040808.JPG,0.170159
C:/Users/Server/Pictures/P1040819.JPG,0.885534
C:/Users/Server/Pictures/P1040825.JPG,0.11393
C:/Users/Server/Pictures/P1040826.JPG,0.0536503
等等。我发现一个php脚本,它在我的浏览器中显示这些结果,如下所示:

<?php
$row = 1;
if (($handle = fopen("C:/xampp/htdocs/results/result.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo " ","Record #",$row," "; $row++;
        for ($recordcount=0; $recordcount < $num; $recordcount++) {
            echo $data[$recordcount] . " \n";
        }
    }
    fclose($handle);
} ?>

但当它显示csv时,布局完全混乱,例如一行中有多个结果

我想做三件事-

-将0.xx转换为百分比 -将百分比值放在第一列,路径放在第二列 -最重要的是,只需将数据整齐地堆叠起来,使每一行都有一个结果


这些都可以用php完成吗?有人能告诉我,我必须调整什么参数才能得到每行一个结果,而不是更多吗?

不是那么难,检查这里

if (($handle = fopen("yourfile.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
            $percentaje=$data[1]*100;
            echo $percentaje."%&nbsp".$data[0]. "<br>";
    }
    fclose($handle);
} 

您正在将文本转储到浏览器中。它将被浏览器规则转换为html,这意味着如果你想换行,你需要输入

。是的,您的计算可以在PHP中完成。这只是个数学问题。用“
”替换“\n”,试试这个:
echo nl2br($CSV)
16.3533% C:/Users/Server/Pictures/P1040805.JPG
17.0159% C:/Users/Server/Pictures/P1040808.JPG
88.5534% C:/Users/Server/Pictures/P1040819.JPG
11.393% C:/Users/Server/Pictures/P1040825.JPG
5.36503% C:/Users/Server/Pictures/P1040826.JPG