Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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
Javascript PHP JS如何搜索CSV文件列的最大值_Javascript_Php - Fatal编程技术网

Javascript PHP JS如何搜索CSV文件列的最大值

Javascript PHP JS如何搜索CSV文件列的最大值,javascript,php,Javascript,Php,如果可以的话,我想问一下如何使用PHP或Javascipt搜索CSV文件列的最大值 这是我的csv文件表: image,category,prob "data/test1.jpg",zero1,0.947648 "data/test1.jpg",zero1,0.957323 "data/test1.jpg",zero1,0.955677 "data/test1.jpg",zero1,0.951940 "data/test1.jpg",zero1,0.950025 我想找到prob列的最高值并打

如果可以的话,我想问一下如何使用PHP或Javascipt搜索CSV文件列的最大值

这是我的csv文件表:

image,category,prob
"data/test1.jpg",zero1,0.947648
"data/test1.jpg",zero1,0.957323
"data/test1.jpg",zero1,0.955677
"data/test1.jpg",zero1,0.951940
"data/test1.jpg",zero1,0.950025
我想找到prob列的最高值并打印它的类别值

我所寻找的输出如下所示:

zero1,0.957323
感谢您在PHP中提供的帮助,您可以使用这些帮助读取数据:

$handle = fopen("test.csv", "r");
// read the headers
$data = fgetcsv($handle);
// read the data
$max_prob = 0;
$category_max = '';
while (($data = fgetcsv($handle)) !== false) {
    if ($data[2] > $max_prob) {
        $max_prob = $data[2];
        $category_max = $data[1];
    }
}
echo "max prob value is $max_prob for category $category_max\n";
在PHP中,您可以用来读取数据:

$handle = fopen("test.csv", "r");
// read the headers
$data = fgetcsv($handle);
// read the data
$max_prob = 0;
$category_max = '';
while (($data = fgetcsv($handle)) !== false) {
    if ($data[2] > $max_prob) {
        $max_prob = $data[2];
        $category_max = $data[1];
    }
}
echo "max prob value is $max_prob for category $category_max\n";

好的,通常情况下,你要说你尝试了什么,犯了什么错误等等,否则你的问题可能会被删除。但是,这里有:

var originalString = `"data/test1.jpg",zero1,0.947648
    "data/test1.jpg",zero1,0.957323
    "data/test1.jpg",zero1,0.955677
    "data/test1.jpg",zero1,0.951940
    "data/test1.jpg",zero1,0.950025`;

var oneDArray = originalString.split('\n\r'); // split on the return characters, you may have to play with this to get it to work.

var twoDAttay = oneDArray.map( line => line.split(",") ); // make an array of 3 elements for each line, split by the commas.
因此,在这一点上,你有:

twoDArray = [
    ["data/test1.jpg",zero1,0.947648],
    ["data/test1.jpg",zero1,0.957323],
    ["data/test1.jpg",zero1,0.955677],
    ["data/test1.jpg",zero1,0.951940],
    ["data/test1.jpg",zero1,0.950025]
];
所有数字都在每个内部数组的
2
位置。现在,我们已经准备好查找最大数量,我们只需要在它们自己的数组中获取数字:

var numArray = twoDArray.map( line => Number( line[2] )); // get only the numbers
var maxNum = Math.max( ...numArray );   // find the maximum number
var index = numArray.indexOf( maxNum ); // find the index of the max number, this will correspond to the oneDArray.
var maxLine = oneDarray[ index ];       // This is your answer

但在获得二维阵列后,有很多方法可以做到这一点:。使用对您最有意义的版本。最困难的部分是将数据清理成js可以理解的格式-那些带引号和不带引号的字符串将是一个问题。

好的,所以通常情况下,您要说您尝试了什么,出现了什么错误等,否则您的问题可能会被删除。但是,这里有:

var originalString = `"data/test1.jpg",zero1,0.947648
    "data/test1.jpg",zero1,0.957323
    "data/test1.jpg",zero1,0.955677
    "data/test1.jpg",zero1,0.951940
    "data/test1.jpg",zero1,0.950025`;

var oneDArray = originalString.split('\n\r'); // split on the return characters, you may have to play with this to get it to work.

var twoDAttay = oneDArray.map( line => line.split(",") ); // make an array of 3 elements for each line, split by the commas.
因此,在这一点上,你有:

twoDArray = [
    ["data/test1.jpg",zero1,0.947648],
    ["data/test1.jpg",zero1,0.957323],
    ["data/test1.jpg",zero1,0.955677],
    ["data/test1.jpg",zero1,0.951940],
    ["data/test1.jpg",zero1,0.950025]
];
所有数字都在每个内部数组的
2
位置。现在,我们已经准备好查找最大数量,我们只需要在它们自己的数组中获取数字:

var numArray = twoDArray.map( line => Number( line[2] )); // get only the numbers
var maxNum = Math.max( ...numArray );   // find the maximum number
var index = numArray.indexOf( maxNum ); // find the index of the max number, this will correspond to the oneDArray.
var maxLine = oneDarray[ index ];       // This is your answer

但在获得二维阵列后,有很多方法可以做到这一点:。使用对您最有意义的版本。最困难的部分是将数据清理成js可以理解的格式-那些带引号和不带引号的字符串将成为一个问题。

@irongant无需担心。很高兴我能帮忙,不用担心。很高兴我能帮忙。