1

I have the following array

Array
(
    [0] => Array
        (
            [title] => Title of company1
            [link] => https://companywebsite1.com
            [result] => 29.814814815
        )

    [1] => Array
        (
            [title] => Title of company2
            [link] => https://companywebsite2.com
            [result] => 143.723259762
        )

    [2] => Array
        (
            [title] => Title of company3
            [link] => https://companywebsite3.com
            [result] => 212.202797203
        )

    [3] => Array
        (
            [title] => Title of company4
            [link] => https://companywebsite4.com
            [result] => 127.884615385
        )

    [4] => Array
        (
            [title] => Title of company5
            [link] => https://companywebsite5.com
            [result] => 175.911330049
        )

)

How can I get the parent key of the company with the highest result value? For example a function that would return key 2 because the maximum value here is 212.20

I tried this but it only returns me the maxium value not the key. How can i get the parent key of the company with the maximum result value?

function maxMarks($array) {
    $max=0;
    foreach ($array as $Rsult) {
        $max=$Rsult['result']>$max  ?$Rsult['result']:$max;
    }
    return $max;
}
echo maxMarks($result);

1 Answer 1

3

Try like

function maxMarks($array) {
    $max=0;
    $max_key = '';
    foreach ($array as $key=>$Rsult) {
        $max = $Rsult['result'] > $max ? $Rsult['result'] : $max;
        $max_key = $Rsult['result'] > $max ? $key : $max_key;
    }
    return $max_key;
}
echo maxMarks($result);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Gautam3164, I will accept this as the correct answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.