Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP find second highest element value in a multidimensional array

Tags:

arrays

php

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

$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
            )

    )
like image 687
RaduS Avatar asked Jul 28 '26 06:07

RaduS


1 Answers

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

like image 157
idmean Avatar answered Jul 30 '26 19:07

idmean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!