如果其中一个元素为零,如何在多个数组中找到min float值

时间:2015-02-04 10:07:54

标签: php

这是我的多个数组

Array
(
    [3432] => Array
        (
            [2] => 7.2
            [3] => 1.5
            [4] => 2.1
        )

    [11350] => Array
        (
            [2] => 23.168
            [3] => 4.344
            [4] => 4.344
        )

    [12175] => Array
        (
            [2] => 8.112
            [3] => 6.288
            [4] => 4.344
        )

    [12306] => Array
        (
            [2] => 9.036
            [3] => 0.9
            [4] => 0
        )

)

我需要找到一个最小值。 当我使用php min函数时,我得到10.8

Array
(
    [2] => 7.2
    [3] => 1.5
    [4] => 2.1
)

10.8

但所有数组的最小值为9.936

[12306] => Array
        (
            [2] => 9.036
            [3] => 0.9
            [4] => 0
        )

1 个答案:

答案 0 :(得分:3)

试试这个:

<?php
$input = array(
    11350 => array(
        2 => 23.168,
        3 => 4.344,
        4 => 4.344,
    ),
    12175 => array(
        2 => 8.112,
        3 => 6.288,
        4 => 4.344,
    ),
    12306 => array(
        2 => 9.036,
        3 => 0.9,
        4 => 0,
    ),
);

$sums = array();
foreach ($input as $row) {
    $sums[] = array_sum($row);
}

print_r(min($sums));

修改

或更多PHP方式:

print_r(min(array_map('array_sum', $input)));