简体   繁体   English

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

[英]PHP find second highest element value in a multidimensional array

How can i get the second highest element value in a multidimensional array. 如何在多维数组中获得第二高的元素值。 I use this function to get the highest value. 我使用此函数来获得最高价值。 But how can i find the second highest. 但我怎么能找到第二高的。

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;
}

For example if i call get_max($transactions) i get the array number 4 because it has the highest value: 5 in the transaction_no . 例如,如果我调用get_max($transactions)我得到数组4,因为它具有最高值: transaction_no中为5。 But how can i get the second highest? 但我怎么能获得第二高? for example when i call for get_second_max($transactions) i should get array number 3 because it has 4 in the transaction_no. 例如,当我调用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
            )

    )

When a larger value than current max value is found, you know that the current max value is the 2nd largest till now. 当找到比当前最大值更大的值时,您知道当前最大值是迄今为止的第二大值。 Or if there's a larger value found which is less than or equal to current max value while larger than the current second largest, update the current second largest accordingly. 或者,如果找到的值大于或等于当前最大值但大于当前第二大值,则相应地更新当前第二大值。

Updated Code: 更新的代码:

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;
}
<?php
function get_max($array) {

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

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

To return the array and not only the value: 要返回数组而不仅仅是值:

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:获取关联数组的第n项

For testing purposes your given array 用于测试目的的给定数组

$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',
    ),
);

A simple function to get the n-th ancestor of the maximum transaction number. 获取最大事务数的第n个祖先的简单函数。

It retrieves your array with the transactions and the index of the ancestor you want. 它使用您想要的事务和祖先索引检索您的数组。 Internally it checks wether your array is large enough to get the defined ancestor, sorts the given array by the transaction number and returns the n-th (ancestor-th) element. 在内部,它会检查您的数组是否足够大以获取定义的祖先,通过事务编号对给定数组进行排序并返回第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];
}

The relevant call to get the second highest element 获得第二高元素的相关调用

$secondHighest = getTransactionMaxAncestor($transactions, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM