K
K
kimqar342534262021-06-06 22:04:39
PHP
kimqar34253426, 2021-06-06 22:04:39

Php how to write this algorithm?

There is an array:

Array
(
    [2] => 1
    [4] => 3
    [5] => 1
    [1] => 2
)

It is necessary to compare all the elements and display the number (key) in which the value is greater than the others, but if the largest number in the array is several, then display 0, how to implement this?

For example in this array
Array
(
    [2] => 1
    [4] => 3
    [5] => 1
    [1] => 2
)


Output [4] (because the largest value in the array is 3 under the key [4])

And in the array

Array
(
    [2] => 1
    [4] => 3
    [5] => 3
    [1] => 2
)

Output 0 (because the largest number is 3 and it occurs several times)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Spartak (Web-StyleStudio), 2021-06-06
@kimqar34253426

First, find the maximum value using the max function , then you can use the array_keys function with the search_value parameter (the found value through max), as a result, you will get an array of keys with the found value, and finally, the count function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question