Select higher PHP value

3

I have the following:

$gols = array("Kraken"=>"25",        "Cleiton.Xavier"=>"10",     "Buffon"=>"0",

I wanted to find the highest value of $gols and the name of the player also how do I?

    
asked by anonymous 19.11.2018 / 12:33

3 answers

2

You can use the arsort PHP function to sort the array in> $gols and thus get the first value of it, which will be the largest. Try this:

$gols = [
    "Kraken" => "25"
    "Cleiton.Xavier" => "10",
    "Buffon" => "50"
];

arsort($gols);
var_dump($gols);

Return:

  

array (3) {["Buffon"] = > string (2) "50" ["Kraken"] = > string (2) "25" ["Cleiton.Xavier"] = > string (2) "10"}

Now you have in the first index of the array the person with the highest number of goals.

    
19.11.2018 / 13:04
3

Use the max() function to return the highest value and array_search() to search the array for the name of who has this number of goals.

Example:

$gols = array("Kraken"=>"25",        "Cleiton.Xavier"=>"10",     "Buffon"=>"50");

$maiorGol  = max($gols); *//Retorna 50*
$maiorNome = array_search ($maiorGol,$gols); *//Retorna Buffon*
    
19.11.2018 / 12:59
0

To return greater value - > function max()

$gols = array("Pequnion"=>"25","Minimion"=>"10","Zeron"=>"0","Grandon"=>"100","Medion"=>"50");

echo max($gols);

$maxs = array_keys($gols, max($gols));

echo ($maxs[0]);
Para retornar a chave do maior valor, uso array_keys
pois pode haver vários valores máximos iguais 
     

Example in ideone with two equal maximum values

array_keys - the keys to positions where a given value is find.

  

example:

$arr = array("determinado valor", "um valor", "outro valor", "determinado valor", "determinado valor");
var_dump(array_keys($arr, "determinado valor));
  

returns

array(3) {
  [0]=>
  int(0)
  [1]=>
  int(3)
  [2]=>
  int(4)
}
  

Because " determinado valor " is present in positions 0, 3 and 4 of the array.

    
19.11.2018 / 17:44