douyu7210 2016-10-10 03:46
浏览 45
已采纳

使用Array_filter将大关联数组转换为更小的关联数组

I'm working on calculating the pass ratings of various football players. I have a .txt file that I'm pulling the info from.

while($line = fgetcsv($fp, 255, ',')){
$rate = round(calcPR($line),2);
$rating[$line[0]] = $rate;
} return $rating;

That was the portion of my function where I'm using a $fp to read from my .txt file and here is where I'm trying to display the data, but before I attempt to display my data I try to split the data into pass ratings that are great,good,mediocre, and poor.

For a great rating, they need to be above 95 so I have:

$grtRating = array_filter($rating,function(){
       return $rating > 95;
});

The rest of my code for good, mediocre, and poor looks just about the same, but with different criteria. How can I get this $grtRating array to store only the scores that are above 95?

Currently when I run my program it basically ignores the operators and displays all of the ratings no matter how low.

UPDATE: Output of $poorRating array: (This is everything < 86)

Array ( [Cody Kessler] => 85.91 [Kirk Cousins] => 82.34 [Jacoby   Brissett] => 81.68 [Ryan Tannehill] => 81.2 [Tyrod Taylor] => 79.29 [Ben   Roethlisberger] => 77.49 [Shaun Hill] => 77.32 [Carson Palmer] => 74.79 [Jameis   Winston] => 73.93 [Marcus Mariota] => 73.06 [Joe Flacco] => 71.77 [Cam Newton]  => 70.32 [Josh McCown] => 70.25 [Trevone Boykin] => 69.2 [Jay Cutler] => 68.46  [Blake Bortles] => 67.13 [Brock Osweiler] => 66.1 [Blaine Gabbert] => 63.35  [Case Keenum] => 60.63 [Ryan Fitzpatrick] => 48.93 [Robert Griffin III] => 48.54  [Drew Stanton] => 34.36 [Kellen Clemens] => 2.07 )

I think the problem might be in how I output my code then. I have my links in an unordered list.

Here is an example:

<a href='pr.php?action=all'>All Ratings</a>

I have a pr.php?action=great',pr.php?action=good', pr.php?action=mediocre', andpr.php?action='poor'.

Here is how I output everything, is the problem how I do that?

$mode = 'all';

if ($_GET['action'] == 'great') $mode = 'great';
if ($_GET['action'] == 'good') $mode = 'good';
if ($_GET['action'] == 'mediocre') $mode = 'mediocre';
if ($_GET['action'] == 'poor') $mode = 'poor';

    if($mode == 'great'){
                    foreach($greatRating as $name=>$pr){
                            echo "<tr><td>{$name}</td><td>{$pr}</td></tr>
";
                    }
            }
           if($mode == 'good'){
                    foreach($goodRating as $name=>$pr){
                    echo "<tr><td>{$name}</td><td>{$pr}</td></tr>
";
                    }
             }
           if($mode == 'mediocre'){
                    foreach($mediocreRating as $name=>$pr){
                    echo "<tr><td>{$name}</td><td>{$pr}</td></tr>
";
                    }
            }
           if($mode == 'poor'){
                    foreach($poorRating as $name=>$pr){
                    echo "<tr><td>{$name}</td><td>{$pr}</td></tr>
";
                    }
            }
            if($mode = 'all'){
                    foreach($rating as $name=>$pr){
                    echo "<tr><td>{$name}</td><td>{$pr}</td></tr>
";
                    }
            }


    echo "</table></div>
";
}

Would you please help me figure out where my error is?

  • 写回答

1条回答 默认 最新

  • dongnianchou7047 2016-10-10 03:53
    关注

    Replace

    $grtRating = array_filter($rating,function(){
        return $rating > 95;
    });
    

    with

    $grtRating = array_filter($rating, function($val) {
       return $val > 95;
    });
    

    In the first one, you did not include the callback argument that contains the value passed from each iteration of the array, which will be the one used in your evaluation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试