I can not write an algorithm in php to get several keys from the array with the maximum value. My wrong example:

$a = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 4, 'f' => 3, 'g' => 5, 'h' => 5, 'i' => 2, 'j' => 1, ); for($i = 0; $i < $count; $i++) { $b = array_keys($a, max($a)); здесь, мне надо удалить из массива а ключ, чтобы не брать его снова. } 

There is another catch, I need to get the number of keys equal to the count, but there may be several identical maximum values ​​in the array (since in my case these will be dates).

It turns out that if with count 3, I chose the values ​​(4,4,3,2). I need to delete 2. With (4,3,3,2) - randomly one triple.

In general, get from the array N-th number of records, taking into account the same values.

  • look at array_count_values ​​and sort - next is simple. !!! Even array_keys () with the second parameter is splash58
  • Googled about the usort function. Thank. - user3510544 2:51 pm
  • I do not see usort in this task - splash58

1 answer 1

Try this:

 $a = [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 4, 'f' => 3, 'g' => 5, 'h' => 5, 'i' => 2, 'j' => 1, ]; $count = 3; // Сколько значений нужно выбрать arsort($a); // Сортируем массив в обратном порядке $a = array_unique($a); // Убираем дубликаты $a = array_slice($a, 0, $count); // Выбираем нужное количество элеиментов 
  • I did the same before your answer, thanks :) - user3510544