PHP CSV最大数量

时间:2017-11-04 16:07:08

标签: php csv

所以我用我的代码打开一个CSV文件,我得到了这个输出:

    Array
(
    [0] => Array
        (
            [0] => Possibilities
            [1] =>  First
            [2] =>  Second
            [3] =>  Third
            [4] =>  Forth
        )

    [1] => Array
        (
            [0] => Lower chance
            [1] =>  28
            [2] =>  24
            [3] =>  16
            [4] =>  30
        )

    [2] => Array
        (
            [0] => Higher chance
            [1] =>  30
            [2] =>  42
            [3] =>  44
            [4] =>  34
        )

)

现在我遇到了问题,因为我无法弄清楚如何从[1] =>数组和[2] =>数组中获取MAX数。如何计算两个数组之间的平均数,如[1] [1]和[2] [1](平均值为28 + 30)?

1 个答案:

答案 0 :(得分:0)

$arr1 = $array[1];//get second array from the variable contain the arrays.
$arr2 = $array[2];//get third one from the variable contain the arrays.

echo max($arr1) . ' : ' . max($arr2) . '<br>';//Max 2 numbers in the both arrays.
echo $arr1[1], '+', $arr2[1], ' = ', $arr1[1]+$arr2[1], '<br>';//count first 2 elements from the first array.
echo $arr1[2], '+', $arr2[2], ' = ', $arr1[2]+$arr2[2], '<br>';//count first 2 elements from the second array.
echo $arr1[1], '+', $arr2[1], ' + ', $arr1[2], '+', $arr2[2], ' = ', $arr1[1]+$arr2[1]+$arr1[2]+$arr2[2];//count first 2 elements from both arrays.

试试这个。

<强> EDITED

代码对我来说很好,但如果获取整数元素有任何问题, 只需使用filter_var_array()

即可
$arr1 = filter_var_array($array[1], FILTER_SANITIZE_NUMBER_INT);
$arr2 = filter_var_array($array[2], FILTER_SANITIZE_NUMBER_INT);