使用cURL:
$url = $some_site;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
其中'$ output'变量包含以下7列(行数将有所不同):
1,A,6045,6168,6731,6847,522800
2,B,7847,8124,7645,7716,614400
3,C,7288,7633,7150,7442,801800
4,d,5546,5791,5460,5581,554200
5,E,4579,4679,4359,4572,557400
等等......如您所见,在第4列中,数字为:
6168 8124 7633 5791 4679
最高的数字是:8124
我试图弄清楚如何解析'$ output'变量,以便我可以评估第4列中的所有数字以确定高位。
我试过了:
$csv = array_map('str_getcsv', $output);
echo max($csv[4]);
但没有回复
答案 0 :(得分:0)
如果输出$csv
值,您会看到它是具有以下结构的数组:
...
[2] => Array
(
[0] => 3
[1] => c
[2] => 7288
[3] => 7633
[4] => 7150
[5] => 7442
[6] => 801800
)
[3] => Array
(
[0] => 4
[1] => d
[2] => 5546
[3] => 5791
[4] => 5460
[5] => 5581
[6] => 554200
)
[4] => Array
(
[0] => 5
[1] => e
[2] => 4579
[3] => 4679
[4] => 4359
[5] => 4572
[6] => 557400
)
....
因此,您的$csv[4]
不来自第4列的值列表。它是数组的第5个元素。因此,您需要更进一步,使用简单的foreach
:
$max = 0;
foreach ($csv as $row) {
// use index `3` because numeration starts with `0`
if ($max < $row[3]) {
$max = $row[3];
}
}
或使用array_column
(因为 php5.5 ):
// take all values witn index `3` and find max
echo max(array_column($csv, 3));
在这两种情况下都是8124
答案 1 :(得分:0)
这是因为str_getcsv没有遵循标准,而且输出你有一个“破碎的阵列”...我想它可能看起来像这样:
Array
(
[0] => 1
[1] => a
[2] => 6045
[3] => 6168
[4] => 6731
[5] => 6847
[6] => 522800
2
[7] => b
[8] => 7847
[9] => 8124
[10] => 7645
[11] => 7716
[12] => 614400
3
[13] => c
[14] => 7288
[15] => 7633
[16] => 7150
[17] => 7442
[18] => 801800
4
[19] => d
[20] => 5546
[21] => 5791
[22] => 5460
[23] => 5581
[24] => 554200
5
[25] => e
[26] => 4579
[27] => 4679
[28] => 4359
[29] => 4572
[30] => 557400
)
http://sandbox.onlinephpfunctions.com/code/b5834d98eda11b1726adc9ef1b7592f9ba32242c
你可以这样做:
<?php
// $csv is just a string ... output from Your curl will be same I think
$csv = <<<EOT
1,a,6045,6168,6731,6847,522800
2,b,7847,8124,7645,7716,614400
3,c,7288,7633,7150,7442,801800
4,d,5546,5791,5460,5581,554200
5,e,4579,4679,4359,4572,557400
EOT;
function csv2array($csv) {
$csv = str_getcsv($csv, PHP_EOL);
return array_map('str_getcsv', $csv);
}
function findMaxInColumn($array, $column = 3) {
return max(array_column($array, $column));
}
$parsedCsv = csv2array($csv);
print_r(findMaxInColumn($parsedCsv));
http://sandbox.onlinephpfunctions.com/code/82020f0d85acf8d6b15b5e7fd78a8d1a80618ff2