$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]
];
所以我的问题是,我怎样才能获得每个角色的最高品质。就像Character1一样,它要么是智慧,要么是智慧。我想重复这个角色的名字,最好的质量和与质量相关的价值。
foreach($attributs as $s_key => $value)
{
if($value > $max)
$max = max($attributs);
$maxkey = $s_key;
}
echo "<li>$max , $maxkey</li>";
echo "</ul>";
我也有这段代码,但它只给出了每个字符的最高数字,而没有给出与数字相关的质量的名称。
foreach($characters as $names => $quality){
echo "$names";
echo max($quality);
}
我知道他们根本没有工作,但这是我所能接近我想要的。
发布于 2022-09-21 21:26:14
您可以使用max
函数找到数组的最高值,同时也可以使用array_search()
找到键。如果你有两个具有相同价值的技能,假设“智能”为6,而“智慧”为6,则这将失败,只会显示“智能”。你必须同时给他们看还是只给他们看一张?
$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 ) )
发布于 2022-09-21 16:17:16
对每个字符使用max
和array_search
的组合以得到最高的
或者,使用temp对象我们将“保存”最高的键和值。演示中显示了这个额外的循环,但如果您在echo
之后移动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;
}
输出:
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
注意事项:如果有相同(最高)值的多个键,将使用第一个键!
发布于 2022-09-21 16:54:12
虽然Ostone0
的方法是正确的,但我认为它不需要这么复杂。只要尝试下面的方法就能得到你想要的。这更容易,也更直接。
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 ;
}
--这将输出以下
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
https://stackoverflow.com/questions/73803824
复制相似问题