PHP在多维数组中找到第二大元素值

标签 php arrays

如何获取多维数组中第二高的元素值。 我使用这个函数来获得最高值。但是我怎样才能找到第二高的。

function get_max($array)
{
    $max = -999999999;
    $found_item = null;

    foreach ($array as $key => $value) {
        if ($value['transaction_no'] > $max) {
           $max = $value['transaction_no'];
           $found_item = $value;
        }
    }

    return $found_item;
}

例如,如果我调用 get_max($transactions),我会得到数组编号 4,因为它具有最高值:transaction_no 中的 5。但是我怎样才能获得第二高呢?例如,当我调用 get_second_max($transactions) 时,我应该得到数组编号 3,因为它在 transaction_no 中有 4。

$transactions = Array
    (
        [0] => Array
            (
                [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506
                [transaction_no] => 1
                [transaction_total_amount] => 589.00
                [transaction_date] => 1335932419
                [transaction_status] => cancelled
            )

        [1] => Array
            (
                [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d
                [transaction_no] => 2
                [transaction_total_amount] => 79.00
                [transaction_date] => 1336476696
                [transaction_status] => cancelled
            )

        [2] => Array
            (
                [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d
                [transaction_no] => 3
                [transaction_total_amount] => 299.00
                [transaction_date] => 1336476739
                [transaction_status] => cancelled
            )

        [3] => Array
            (
                [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d
                [transaction_no] => 4
                [transaction_total_amount] => 79.00
                [transaction_date] => 1336476927
                [transaction_status] => cancelled
            )

        [4] => Array
            (
                [transaction_user_id] => 8e9050a3646c98342b9ba079fba80982
                [transaction_no] => 5
                [transaction_total_amount] => 129.00
                [transaction_date] => 1336477032
                [transaction_status] => cancelled
            )

    )

最佳答案

<?php
function get_max($array) {

    $all = array();
    foreach($array as $key=>$value){
        $all[] = $value['transaction_no'];
    }

    rsort($all);
    return $all[1];
}

要返回数组而不仅仅是值:

function get_max($array) {

    $all = array();
    foreach($array as $key=>$value){
        /* creating array where the key is transaction_no and
           the value is the array containing this transaction_no */
        $all[$value['transaction_no']] = $value;
    }

    /* now sort the array by the key (transaction_no) */
    krsort($all);

    /* get the second array and return it (see the link below) */
    return array_slice($all, 1, 1)[0];
}

PHP: Get n-th item of an associative array

关于PHP在多维数组中找到第二大元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24981378/

相关文章:

javascript - 如何使用 jQuery 调用某个数组中的每个方法

c++ - 我被这段使用指针访问二维数组中的值的代码所困扰

php - 将 css 应用于数组中的特定元素

php - Yii "framework"子文件夹在 apache 中不可见? [错误 403]

php - 如何删除所有换行符并从 PHP 中的字符串中转义字符?

java - Java 中的 For 循环和数组

java - 在二维数组中查找重复值

PHP 使用 xpath 输出

php - 如果我在执行函数中绑定(bind)参数,我的PDO准备语句是否安全?

php - mysql 查询连接和速度/替代数据库解决方案是否必要?