如何从php数组中提取值

时间:2017-05-23 06:46:57

标签: php

我试图返回与“减少”相关联的最大数值。下面的数组。如果有一个值但很多有两个值,则足够容易 - 如下所示。我正在使用

$reduced_array = $data['rates'][1]['rates'][0];

但这仅适用于返回第一个值。我需要返回最高值但是低于8。 会像

 if(count($data['rates'][1]['rates']) > 2) {
        ***return largest value here***

工作?我只是不确定如何执行带星号的任务 - 也许是for循环?这是阵列。

Array
(
[rates] => Array
    (
        [0] => Array
            (
                [name] => Super Reduced
                [rates] => Array
                    (
                    )

            )

        [1] => Array
            (
                [name] => Reduced
                [rates] => Array
                    (
                        [0] => 5
                        [1] => 8
                    )

            )

        [2] => Array
            (
                [name] => Standard
                [rates] => Array
                    (
                        [0] => 23
                    )

            )

        [3] => Array
            (
                [name] => Increased
                [rates] => Array
                    (
                    )

            )

        [4] => Array
            (
                [name] => Parking
                [rates] => Array
                    (
                    )

            )

    )

[disclaimer] => Rates data is based on information published by the European Commission, updated 1st January 2017.
)

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您可以使用max()

$array = array(
    "rates" => array(
        array(
            "name" => "Super Reduced",
            "rates" => array()
        ),
        array(
            "name" => "Reduced",
            "rates" => array(
                5,
                8
            )
        )
    )
);

echo max($array["rates"][1]["rates"]); // 8

答案 1 :(得分:0)

最简单的解决方案是在内部阵列上使用max 例如:

if(!empty($data['rates'][1]['rates'])) {
    $maxvalue = max($data['rates'][1]['rates']);
}