doufenpaiyu63706 2015-11-24 20:54
浏览 26
已采纳

如何将数组的某些值一起添加,然后按总和进行排序?

So I am trying to find the best model within our database to match the user's inputted values for Height, Weight, etc. I have generated a variable called heightMatchMultiple based on weighted values and stored all the results in a global array $models. How can I apply math to only the ____MatchMultiple fields and then sort by the result? For example, I store the modelID of each model but I only want to add up the other values and then find the highest average.

Here is some relevant code:

while($row=mysqli_fetch_array($array))
{
    $userHeight=$_POST['height'];
    $userWeight=$_POST['weight'];
    $userShoulder=$_POST['shoulder'];
    $userWaist=$_POST['waist'];
    $userInseam=$_POST['inseam'];
    $heightMatchMultiple=0;

    //creates a weighted variable for height
    if(isset($row['modelHeight']))
    {
        if($userHeight==$row['modelHeight'])
        {
            $heightMatchMultiple=10;
        }
        elseif($userHeight==($row['modelHeight']+1) || $userHeight==($row['modelHeight']-1))
        {
            $heightMatchMultiple=9;
        }
        //same code down til multiple hits 1
        else
        {
            $heightMatchMultiple=1;
        }

        //similar code for the weight, shoulders, etc.......
        //....
        array_push($models,array("modelID" => $row['modelID'],
                                 "modelHeightMatch" => $heightMatchMultiple,
                                 "modelWeightMatch" => $weightMatchMultiple,
                                 "modelShoulderMatch" => $shoulderMatchMultiple,
                                 "modelWaistMatch" => $waistMatchMultiple,
                                 "modelInseamMatch" => $inseamMatchMultiple));

I would like to make a function that adds all of the _____MatchMultiple variables within the array (not including modelID) and then divides by the number of items within the array for each. This way I can find out which model is the closest match.

  • 写回答

2条回答 默认 最新

  • doutuo1908 2015-11-24 21:10
    关注
    function getAverage($array) {
    
         var $myTotal = 0
    
         for ($i = 1; $i<= sizeof($array) - 1; $i++) {
    
             $myTotal += $array[$i]
    
         }
    
         var $myAverage = $myTotal/(sizeof(array) - 1)
    
         return($myAverage)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题