V
V
Vlad_isLove2019-05-25 19:09:47
PHP
Vlad_isLove, 2019-05-25 19:09:47

How to get the name of the weapon with the most shots in PHP if the weapons are represented as a multidimensional array?

There is such an array with weapons:

Array
(
    [pistol] => Array
        (
            [name] => Пистолет
            [shots] => 12
        )
    [shotgun] => Array
        (
            [name] => Дробовик
            [shots] => 8
        )
    [rifle] => Array
        (
            [name] => Автомат
            [shots] => 34
        )
)

There are a lot of weapons.
Question: How to get the name [name] of the weapon that has the most shots [shots]?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-05-25
@Vlad_isLove

$result = array_reduce(
  $array, 
  function($carry, $item) {
    return ($item['shots'] > $carry['shots']) ? $item : $carry;
  },
  ['name' => '', 'shots' => -1]
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question