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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程