CSGuy94 CSGuy94 - 4 months ago 20
PHP Question

Big Associative Array into Smaller Associative Arrays using 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', and
pr.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>\n";
}
}
if($mode == 'good'){
foreach($goodRating as $name=>$pr){
echo "<tr><td>{$name}</td><td>{$pr}</td></tr>\n";
}
}
if($mode == 'mediocre'){
foreach($mediocreRating as $name=>$pr){
echo "<tr><td>{$name}</td><td>{$pr}</td></tr>\n";
}
}
if($mode == 'poor'){
foreach($poorRating as $name=>$pr){
echo "<tr><td>{$name}</td><td>{$pr}</td></tr>\n";
}
}
if($mode = 'all'){
foreach($rating as $name=>$pr){
echo "<tr><td>{$name}</td><td>{$pr}</td></tr>\n";
}
}


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


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

Answer

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.