Home > Software design >  How to find the highest value in a associative array in php
How to find the highest value in a associative array in php

Time:09-22

 $characters = [
        "Character1" => ["Strength" => 1, "Dexterity" => 2,
        "Intelligence" => 6, "Wisdom" => 6, "Charisma" => 3],
        "Character2" => ["Strength" => 5, "Dexterity" => 4,
        "Intelligence" => 1, "Wisdom" => 1, "Charisma" => 6],
        "Character3" => ["Strength" => 6, "Dexterity" => 5,
        "Intelligence" => 5, "Wisdom" => 1, "Charisma" => 5]
        "Character4" => ["Strength" => 1, "Dexterity" => 2,
        "Intelligence" => 4, "Wisdom" => 3, "Charisma" => 3]
        ];

So my question is as follows, how can I get the highest quality of each character. Like for Character1 it would be either Intelligence or Wisdom. I would like to echo the name of the character, the best quality and the value associated with the quality.

 foreach($attributs as $s_key => $value)
                {   
                    if($value > $max)
                        $max =   max($attributs);
                        $maxkey = $s_key;
                }
                echo "<li>$max , $maxkey</li>";
               
                echo "</ul>";

I also have this code, but it only gives me the highest number for each character without giving me the name of the quality associated with the number.

 foreach($characters as $names => $quality){
        echo "$names"; 
        echo max($quality);
        }
        

I know none of them work at all, but that's as close as I could get to what I wanted. Sorry if what I'm asking isn't clear enough, I don't usually ask programming question in English and I'm newbie in php.

CodePudding user response:

You can find the highest value of an array using max function, meanwhile the key using array_search(). This will fail if you have 2 skills with the same value, assuming that "Intellegence" is 6 and "Wisdom" is 6 only "Intellegence" will be shown. Do you have to shown both of them or only one ?

$characters = [
    "Character1" => ["Strength" => 1, "Dexterity" => 2, "Intelligence" => 6, "Wisdom" => 6, "Charisma" => 3],
    "Character2" => ["Strength" => 5, "Dexterity" => 4, "Intelligence" => 1, "Wisdom" => 1, "Charisma" => 6],
    "Character3" => ["Strength" => 6, "Dexterity" => 5, "Intelligence" => 5, "Wisdom" => 1, "Charisma" => 5],
    "Character4" => ["Strength" => 1, "Dexterity" => 2, "Intelligence" => 4, "Wisdom" => 3, "Charisma" => 3]
];

foreach ($characters as $key => $character){
    $value = max($character);
    $skill = array_search($value, $character);
    $characters[$key]['highest_skill'] =  ['skill' => $skill, 'value' => $value];
} 

print_r($characters["Character1"]);
// this will be shown
Array ( [Strength] => 1 [Dexterity] => 2 [Intelligence] => 6 [Wisdom] => 6 [Charisma] => 3 [highest_skill] => Array ( [skill] => Intelligence [value] => 6 ) ) 

CodePudding user response:

Use a combination of max and array_search to get the highest for each character

Optionally, use a temp object were we will 'save' the highest key and value. This extra loop is shown in the demo, but can be removed if you move the echo after the array_search


<?php

    $characters = [
        "Character1" => ["Strength" => 1, "Dexterity" => 2, "Intelligence" => 6, "Wisdom" => 6, "Charisma" => 3],
        "Character2" => ["Strength" => 5, "Dexterity" => 4, "Intelligence" => 1, "Wisdom" => 1, "Charisma" => 6],
        "Character3" => ["Strength" => 6, "Dexterity" => 5, "Intelligence" => 5, "Wisdom" => 1, "Charisma" => 5],
        "Character4" => ["Strength" => 1, "Dexterity" => 2, "Intelligence" => 4, "Wisdom" => 3, "Charisma" => 3]
    ];

    $res = new \stdClass();

    foreach ($characters as $name => $char) {
        $highest = array_search(max($char), $char);
        $res->{$name} = [ $highest, $char[$highest] ];
    }

    foreach ($res as $name => $val) {
        echo "Highest for {$name} is {$val[0]}! Value: {$val[1]}" . PHP_EOL;
    }

Output:

Highest for Character1 is Intelligence! Value: 6
Highest for Character2 is Charisma! Value: 6
Highest for Character3 is Strength! Value: 6
Highest for Character4 is Intelligence! Value: 4

Try it online!


Note: If there are multiple keys with the same (highest) value, the first will be used!

CodePudding user response:

Though Ostone0 is right in his approach, but I assume it does not need to be this complicated. Just try the following to get what you want. It is easier and much more straightforward.

foreach ($characters as $character => $names) {
  $quality = array_search(max($names), $names);
  $max = max($names);
  echo 'The highest value for '. $character . ' is ' . $max . ' for the quality ' . $quality.   PHP_EOL ;
}

This will output the following:

The highest value for Character1 is 6 for the quality Intelligence
The highest value for Character2 is 6 for the quality Charisma
The highest value for Character3 is 6 for the quality Strength
The highest value for Character4 is 4 for the quality Intelligence

Try it online!

  • Related