繁体   English   中英

更改子数组的值

[英]Changing values of a sub array

该程序获取一堆NFL球员的统计信息,计算他们的通过率,然后显示所有信息。 它大部分已经完成,但是我在几个不同的领域遇到了麻烦。

$rate = "";
$file = 'purposely took the file location out';
$fp = fopen($file, 'r');
$name = "";
$team = "";
$completions = "";
$attempts = "";
$yards = "";
$touchdowns = "";
$interceptions = "";

function getNflStats($file){
@ $fp = fopen($file, 'r');


if(!$fp){
    echo "Could not open the file!<br/>";
    exit;
}

$nflStats = array();

while($data = fgetcsv($fp,255,',')){
    $name = $data[0];
    $team = $data[1];
    $completions = $data[2];
    $attempts = $data[3];
    $yards = $data[4];
    $touchdowns = $data[5];
    $interceptions = $data[6];
    $rate = Calculate($completions, $attempts, $yards, $touchdowns, 
    $interceptions);
    $nflStats[] = array("name"=>$rate);
}
fclose($fp);
return $nflStats;
}

如何更改子数组中的值? 在calculate函数中,我创建了一个数组,其中保存了我所有的计算值。 经过多次尝试,我发现由于while循环,每个值$ a,$ b,$ c和$ d都是其自己的数组。 我正在尝试检查任何子数组中的任何值是否大于2.375或小于0,如果是,则将其更改为2.375或0。我已经处理了3个小时,但是尚未找到有关如何遍历每个子数组并更改每个单独元素的解决方案。

function Calculate($completions, $attempts, $yards, $touchdowns, 
$interceptions){
$a = ($completions/$attempts - .3) * 5;
$b = ($yards/$attempts - 3) * .25;
$c = ($touchdowns/$attempts) * 20;
$d = (2.375 - ($interceptions/$attempts *25));

$result = array($a,$b,$c,$d);
echo "$result[0] <br/>";
$size = count($result[0]);
foreach($result as $value){
  for($i=0;$i < $size; $i++){
    if($result[i] > 2.375){
            $result[i] = 2.375;
    }
  }
}
$passRating = round((($a + $b + $c + $d)/6) * 100, 2);

return $passRating;
}

我也很难输入玩家的名字,而且他们的合格率也很高。 我只能制作一个或另一个,但不能同时制作两个。 我不确定是否是我的数组格式或foreach循环的问题。

function displayStats($nflStats){

    asort($nflStats);

    echo "<table>";
    echo "<thead><tr>
       <td><h2>Player</h2></td>
       <td></td>
       <td><h2>Rating</h2></td>
       </tr></thead>";

    foreach($nflStats as $stats){
        echo "<tr>
            <td>{$stats['name']}</td>
            <td></td>
            <td style='text-align:right'></td>
          </tr>";
    }

    echo "</table>";
}

displayStats($nflStats);
?>

这是一些我正在从文件中读取的数据:

Tom Brady,NE,385,581,4577,32,8
Philip Rivers,LAC,360,575,4515,28,10
Matthew Stafford,DET,371,565,4446,29,10
Drew Brees,NO,386,536,4334,23,8
Ben Roethlisberger,PIT,360,561,4251,28,14
Matt Ryan,ATL,342,529,4095,20,12
Kirk Cousins,WAS,347,540,4093,27,13
Alex Smith,KC,341,505,4042,26,5
Russell Wilson,SEA,339,553,3983,34,11
Jared Goff,LA,296,477,3804,28,7
Blake Bortles,JAX,315,523,3687,21,13
Case Keenum,MIN,325,481,3547,22,7
Jameis Winston,TB,282,442,3504,19,11
Derek Carr,OAK,323,515,3496,22,13
Eli Manning,NYG,352,571,3468,19,13
Dak Prescott,DAL,308,490,3324,22,13
Andy Dalton,CIN,297,496,3320,25,12
Cam Newton,CAR,291,492,3302,22,16
Carson Wentz,PHI,265,440,3296,33,7
Marcus Mariota,TEN,281,453,3232,13,15
Joe Flacco,BAL,352,549,3141,18,13
Jacoby Brissett,IND,276,469,3098,13,7
Josh McCown,NYJ,267,397,2926,18,9

当前,关于变量i或仅显示值,我遇到了很多错误:

值样本:(试图使它们不为负或超过〜158)

-12.8
-11.25
27.08
31.25
31.77
35.42
46.65
55.06
55.85
60.5
63.39
65.79
66.39
68.15
69.23
70.6
71.44
71.92
71.92
71.99
72.34
72.47
72.53
73.3
75.61
76.37
76.88
77.5 172.92
206.25
214.58
254.17
268.75
577.08
631.25

您忘了在几个地方把美元符号放在i前面,但是我主要是重写了您的代码以尝试为您简化代码。 asort()正在订购ASC并保留密钥-但是您不需要保留密钥。 如果要使用我的新usort()方法进行ASC排序,只需反转太空飞船运算符( <=> )两侧的$a$b变量。

代码:( 演示

$csv=<<<CSV
Tom Brady,NE,385,581,4577,32,8
Philip Rivers,LAC,360,575,4515,28,10
Matthew Stafford,DET,371,565,4446,29,10
Drew Brees,NO,386,536,4334,23,8
Ben Roethlisberger,PIT,360,561,4251,28,14
Matt Ryan,ATL,342,529,4095,20,12
Kirk Cousins,WAS,347,540,4093,27,13
Alex Smith,KC,341,505,4042,26,5
Russell Wilson,SEA,339,553,3983,34,11
Jared Goff,LA,296,477,3804,28,7
Blake Bortles,JAX,315,523,3687,21,13
Case Keenum,MIN,325,481,3547,22,7
Jameis Winston,TB,282,442,3504,19,11
Derek Carr,OAK,323,515,3496,22,13
Eli Manning,NYG,352,571,3468,19,13
Dak Prescott,DAL,308,490,3324,22,13
Andy Dalton,CIN,297,496,3320,25,12
Cam Newton,CAR,291,492,3302,22,16
Carson Wentz,PHI,265,440,3296,33,7
Marcus Mariota,TEN,281,453,3232,13,15
Joe Flacco,BAL,352,549,3141,18,13
Jacoby Brissett,IND,276,469,3098,13,7
Josh McCown,NYJ,267,397,2926,18,9
CSV;

function Calculate($completions, $attempts, $yards, $touchdowns, $interceptions){
    $result=[
        min(2.375, ($completions/$attempts - .3) * 5),
        min(2.375, ($yards/$attempts - 3) * .25),
        min(2.375, $touchdowns/$attempts * 20),
        min(2.375, 2.375 - ($interceptions/$attempts *25))
    ];
    // min() will ensure that no value will exceed 2.375 (avoiding looped conditional entirely)
    return round(array_sum($result)/6*100, 2);  // calculated passingRate
}

// foreach()...explode()...str_csv() is just for demo'ing purposes, you can use your existing process
foreach(explode("\n",$csv) as $line){
    list($name,$team,$completions,$attempts,$yards,$touchdowns,$interceptions)=str_getcsv($line,",");
    $NFL_Stats[]=['team'=>$team,'name'=>$name,'passingRate'=>Calculate($completions, $attempts, $yards, $touchdowns, $interceptions)];
}
usort($NFL_Stats,function($a,$b){return $b['passingRate']<=>$a['passingRate'];});  // order by passingRate DESC
var_export($NFL_Stats);

输出:

array (
  0 => 
  array (
    'team' => 'KC',
    'name' => 'Alex Smith',
    'passingRate' => 104.74,
  ),
  1 => 
  array (
    'team' => 'NO',
    'name' => 'Drew Brees',
    'passingRate' => 103.87,
  ),
  2 => 
  array (
    'team' => 'NE',
    'name' => 'Tom Brady',
    'passingRate' => 102.75,
  ),
  3 => 
  array (
    'team' => 'PHI',
    'name' => 'Carson Wentz',
    'passingRate' => 101.86,
  ),
  4 => 
  array (
    'team' => 'LA',
    'name' => 'Jared Goff',
    'passingRate' => 100.48,
  ),
  5 => 
  array (
    'team' => 'DET',
    'name' => 'Matthew Stafford',
    'passingRate' => 99.33,
  ),
  6 => 
  array (
    'team' => 'MIN',
    'name' => 'Case Keenum',
    'passingRate' => 98.3,
  ),
  7 => 
  array (
    'team' => 'LAC',
    'name' => 'Philip Rivers',
    'passingRate' => 95.96,
  ),
  8 => 
  array (
    'team' => 'SEA',
    'name' => 'Russell Wilson',
    'passingRate' => 95.39,
  ),
  9 => 
  array (
    'team' => 'NYJ',
    'name' => 'Josh McCown',
    'passingRate' => 94.51,
  ),
  10 => 
  array (
    'team' => 'WAS',
    'name' => 'Kirk Cousins',
    'passingRate' => 93.85,
  ),
  11 => 
  array (
    'team' => 'PIT',
    'name' => 'Ben Roethlisberger',
    'passingRate' => 93.37,
  ),
  12 => 
  array (
    'team' => 'TB',
    'name' => 'Jameis Winston',
    'passingRate' => 92.24,
  ),
  13 => 
  array (
    'team' => 'ATL',
    'name' => 'Matt Ryan',
    'passingRate' => 91.36,
  ),
  14 => 
  array (
    'team' => 'DAL',
    'name' => 'Dak Prescott',
    'passingRate' => 86.64,
  ),
  15 => 
  array (
    'team' => 'CIN',
    'name' => 'Andy Dalton',
    'passingRate' => 86.59,
  ),
  16 => 
  array (
    'team' => 'OAK',
    'name' => 'Derek Carr',
    'passingRate' => 86.36,
  ),
  17 => 
  array (
    'team' => 'JAX',
    'name' => 'Blake Bortles',
    'passingRate' => 84.68,
  ),
  18 => 
  array (
    'team' => 'IND',
    'name' => 'Jacoby Brissett',
    'passingRate' => 81.67,
  ),
  19 => 
  array (
    'team' => 'CAR',
    'name' => 'Cam Newton',
    'passingRate' => 80.69,
  ),
  20 => 
  array (
    'team' => 'BAL',
    'name' => 'Joe Flacco',
    'passingRate' => 80.42,
  ),
  21 => 
  array (
    'team' => 'NYG',
    'name' => 'Eli Manning',
    'passingRate' => 80.37,
  ),
  22 => 
  array (
    'team' => 'TEN',
    'name' => 'Marcus Mariota',
    'passingRate' => 79.27,
  ),
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM