0

hi all, how to check same values array if it values is the high values in array associative and get it values?

example 1:
Array
(
[value 1] => 0.8434
[value 4] => 0.8434
[value 6] => 0.8434
[value 3] => 0.3434
[value 2] => 0.3434
[value 7] => 0.2114
)
so, the result:
Array
(
[value 1] => 0.8434
[value 4] => 0.8434
[value 6] => 0.8434
)

example 2:
Array
(
[value 1] => 0.8434
[value 3] => 0.3434
[value 13] => 0.3434
[value 7] => 0.2114
)
so, the result:
Array
(
[value 1] => 0.8434
)

help me please :3

2
Contributors
1
Reply
24
Views
3 Years
Discussion Span
Last Post by cmps
0

Hello,
The concept is:
You have the array (A) with numbers:9,5,5,3
You have to loop through the values in A, and create a variable called max and by default set it to the first value in the array.
Create another array (B) that will hold the index for the max duplicated values, and finally create a counter variable called count.
Now you have to compare each value in the array with the max variable, the three conditions are:
if max > A[i] => do nothing
if max == A[i] => save i in B[count]
if max < A[i] => max = A[i] and count = 0 and B[count] = i

Code:

<?php

    $A = array{9,5,9,5,3};
    $max = $A[0];
    $count = 0;

    for($i=0; $i<count($A);$i++){
        if($A[$i] > $max){
            $max = $A[$i];
            $count = 0;
            $B[$count++] = $i;
        }elseif($A[$i] == $max){
            $B[$count++] = $i;
        }
    }

    for($i=0; $i< $count;$i++){
        echo "Index: ".$B[$i]." => ".$A[$B[i]]."<br/>";
    }
?>

This is just a way to do it, it will print at the end:
index 0 => 9
index 2 => 9

Please note that this code is not tested.
Good Luck

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.