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

时间:2014-07-27 13:21:10

标签: 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
            )

    )

3 个答案:

答案 0 :(得分:1)

当找到比当前最大值更大的值时,您知道当前最大值是迄今为止的第二大值。或者,如果找到的值大于或等于当前最大值但大于当前第二大值,则相应地更新当前第二大值。

更新代码:

function get_max($array) {
    $max = -999999999;
    $max_item = null;
    $second_max_item = null;
    foreach($array as $key=>$value)
    {
        if($value['transaction_no'] >= $max)
        {
            $max = $value['transaction_no'];
            $second_max_item = $max_item;
            $max_item = $value;

        } else if($value['transaction_no'] > $second_max_item['transaction_no']) {

            $second_max_item = $value;

        }
     }

    return $second_max_item;
}

答案 1 :(得分:1)

<?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

答案 2 :(得分:1)

出于测试目的,您的给定数组

$transactions = array(
    array(
        'transaction_user_id'       => '359691e27b23f8ef3f8e1c50315cd506',
        'transaction_no'            => 1,
        'transaction_total_amount'  => 589.00,
        'transaction_date'          => 1335932419,
        'transaction_status'        => 'cancelled',
    ),
    array(
        'transaction_user_id'       => '9def02e6337b888d6dbe5617a172c18d',
        'transaction_no'            => 2,
        'transaction_total_amount'  => 79.00,
        'transaction_date'          => 1336476696,
        'transaction_status'        => 'cancelled',
    ),
    array(
        'transaction_user_id'       => '9def02e6337b888d6dbe5617a172c18d',
        'transaction_no'            => 3,
        'transaction_total_amount'  => 299.00,
        'transaction_date'          => 1336476739,
        'transaction_status'        => 'cancelled',
    ),
    array(
        'transaction_user_id'       => '9def02e6337b888d6dbe5617a172c18d',
        'transaction_no'            => 4,
        'transaction_total_amount'  => 79.00,
        'transaction_date'          => 1336476927,
        'transaction_status'        => 'cancelled',
    ),
    array(
        'transaction_user_id'       => '8e9050a3646c98342b9ba079fba80982',
        'transaction_no'            => 5,
        'transaction_total_amount'  => 129.00,
        'transaction_date'          => 1336477032,
        'transaction_status'        => 'cancelled',
    ),
);

获取最大交易号的第n个祖先的简单函数。

它使用您想要的事务和索引检索您的数组。在内部,它会检查您的数组是否足够大以获取定义的祖先,通过事务编号对给定数组进行排序并返回第n个(祖先)元素。

function getTransactionMaxAncestor($transactions, $ancestor = 0)
{
    if ($ancestor > count($transactions)) {
        throw new Exception('Not enough transactions available');
    }   

    usort($transactions, function ($a, $b) {
        $ta = $a['transaction_no'];
        $tb = $b['transaction_no'];

        if ($a == $b) {
            return 0;
        }

        return $a > $b ? -1 : 1;
    });

    return $transactions[$ancestor];
}

获得第二高元素的相关调用

$secondHighest = getTransactionMaxAncestor($transactions, 1);