php - Properly formatting foreach and if statement -
i'm trying figure out i'm doing wrong here, or if formatted improperly together. getting error on line 3.
foreach($pge['feats'] $val) { } if ($val['name'] == 'wins' && $val['value'] == '1') foreach ($val['value'] == '1' $count) { echo count($count); } here's way tried...
foreach($pge['feats'] $val) if ($val['name'] == 'wins' && $val['value'] == '1') foreach ($val['value'] == '1' $count) { echo count($count); } here's of json i'm using. have several files different values wins/value object. statement go through each 1 , check wins value "1", , add total "1" values together..
partial contents of $pge json
{ "playercount": "2", "remote": "0", "feats": [ { "name": "score", "value": "32" }, { "name": "wins", "value": "0" } ] } thank you!
<?php function game_won(array $game) { $win = false; foreach($game['feats'] $val) { if ($val['name'] == 'wins' && $val['value'] == '1') { $win = true; } } return $win; } function sum_game_wins(array $games) { $sum = 0; foreach($games $game) { if(game_won($game)) { $sum++; } } return $sum; } $game_1 =<<<json { "playercount": "2", "remote": "0", "feats": [ { "name": "score", "value": "32" }, { "name": "wins", "value": "1" } ] } json; $game_2 =<<<json { "playercount": "2", "remote": "0", "feats": [ { "name": "score", "value": "32" }, { "name": "wins", "value": "0" } ] } json; $game_1 = json_decode($game_1, true); $game_2 = json_decode($game_2, true); var_dump(sum_game_wins(array($game_1, $game_2))); output:
int(1)
Comments
Post a Comment