doudu6100 2015-11-23 10:39
浏览 161
已采纳

找到多个变量中最大的变量

I have four variables $a, $b, $c and $d. They each have an integer value assigned to them. I need to find the variable with the highest value (Not the value itself). This is what I tried :

// $a, $b, $c, $d are initialized with values here

$highest_value = max($a,$b,$c,$d);
if($a==$highest_value){$biggest_variable = 'a'};
if($b==$highest_value){$biggest_variable = 'b'};
if($c==$highest_value){$biggest_variable = 'c'};
if($d==$highest_value){$biggest_variable = 'd'};

While not very efficient, it gets the job done. But this falls flat if any of the variables are equal in value. This seemingly simple problem has me stumped! Is there any simpler solution than manually comparing each variable to check if any are equal?

EDIT : Okay so, the reason for sorting is for scoring. Each variable constitutes a Team. Scores are calculated as :

Winning Team = 1,000,000 / Value , Other Teams = 200,000 / Value

So with a = 10 and rest having values 3,4,5 for b,c,d, Score for a is 1,000,000 / 10, Rest would be 200,000 divided by their respective values.

  • 写回答

4条回答 默认 最新

  • dongtuo8170 2015-11-23 10:44
    关注

    You can create an array which you can use to look up the name. Then just use max() as before with the created array and to get the names use array_keys(), e.g.

    <?php
    
        $a = 4;
        $b = 7;
        $c = 2;
        $d = 1;
    
        $arr = ["a" => $a, "b" => $b, "c" => $c, "d" => $d];
    
        print_r(array_keys($arr, max($arr)));
    
    ?>
    

    output:

    Array(
        [0] => b
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集