1

I have an array which looks like this:-


    $my_array = array();
    $my_array[] = array("is_match" => false, "number_of_matches" => 0);
    $my_array[] = array("is_match" => true, "number_of_matches" => 2, "id" => 1);
    $my_array[] = array("is_match" => false, "number_of_matches" => 5, "id" => 1);
    $my_array[] = array("is_match" => false, "number_of_matches" => 3, "id" => 1);
    $my_array[] = array("is_match" => false, "number_of_matches" => 1, "id" => 1);
    

Now i want to get the array with maximum number of matches i.e number_of_matches. Like in this example i want to get below array


    array("is_match" => false, "number_of_matches" => 5, "id" => 1);
    

I know max() function but it returns maximum value in the array but i want to return the array containing the maximum value in number_of_mataches

1
  • usort it, pick the last entry or the first (depending on if you sorted is ascending or descending) of the sorted array. Or a simple foreach loop substituting the current 'winner' with another one if its value is higher. Commented Nov 25, 2014 at 19:24

3 Answers 3

1

If you truly just want the maximum number_of_matches element, then I think uasort is a bit overkill and may potentially take longer to execute (however negligible that difference could be.)

$max = false;
foreach ($my_array as $a) {
    if (!$max || $max['number_of_matches'] < $a['number_of_matches']) {
        $max = $a;
    }
}

Only limitation with this is that it will return the first maximum and no other. To get around that limitation (and this gets lengthier than a uasort but may still be faster than a sort):

$max = false;
foreach ($my_array as $a) {
    if (!$max || $max['number_of_matches'] < $a['number_of_matches']) {
        $max = $a;
        $max_array = array();
        $max_array[] = $a;
    } else if ($max['number_of_matches'] == $a['number_of_matches']) {
        $max_array[] = $a;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

He wants the whole array that holds the max number_of_matches, not just the max number_of_matches.
@MisterDood It still does that. Notice the $max = $a where the conditional compares the element number_of_matches of $max and $a respectively?
how do i get the array in the above code. like there is not echo in the above code. If I want to get that array outside foreach loop then what i can do
@JunaidRehman ... The array will be $max or the array of max arrays will be $max_array. Do with it as your please, if you want to dump them to an echo output then do var_dump($max) or var_dump($max_array)
I just put $max_array; before foreach and at end of foreach i use print_r($max_array)
|
1

You can achieve this with:

usort($my_array, function($a, $b) {
    return $b['number_of_matches'] - $a['number_of_matches'];
});

Comments

1

You can use uasort:

function cmp($a, $b) {
    if ($a['number_of_matches'] == $b['number_of_matches']) {
        return 0;
    }
    return ($a['number_of_matches'] < $b['number_of_matches']) ? 1 : -1;
}

uasort($my_array, 'cmp');

echo $my_array[0]['number_of_matches'];

$my_array[0] will hold the array with the highest number_of_matches

3 Comments

I am getting following error Notice: Use of undefined constant cmp - assumed 'cmp' in D:\xampp\htdocs\testing\check_max.php on line 22 Notice: Undefined variable: a in D:\xampp\htdocs\testing\check_max.php on line 24
@JunaidRehman Oops; the cmp is supposed to be in quotes. I've fixed my answer accordingly. Just change uasort($my_array, cmp); to uasort($my_array, 'cmp');.
@JunaidRehman Oh wow. I didn't test this code before posting it as an answer. Sorry. Change echo $a[0]['number_of_matches']; to echo $my_array[0]['number_of_matches'];

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.