code fragment from a baseball game simulator i developed : method purpose : extracts box score data in real-time from game cache log : by joshua lee php : function get_boxscore_data($occuring = false, $game_id = 0) { $data = array(); // if game id not 0, load specified game data if($game_id != 0){ $this->game_data = $this->get_game_data($game_id); } if( empty($this->game_data) ){ return array(); } $rosters_data_arr = json_decode($this->game_data['game_roster'], true); $pid_team_hash = array(); foreach(array('home', 'away') as $side){ $this_team_id = $rosters_data_arr[$side]['id']; foreach($rosters_data_arr[$side]['roster'] as $player_datum){ $pid_team_hash[$player_datum['id']] = $this_team_id; $data[$this_team_id][$player_datum['id']] = array( 'pos' => $player_datum['pos'] ); if($player_datum['pos'] == 'P'){ $this->pitcher_ids[$side] = $player_datum['id']; } } } // check if gamelog records exist, if not set occuring to false $gamelogs_res = mysql_query("SELECT * FROM gamelogs WHERE game_id = {$this->game_data['game_id']}"); if( mysql_num_rows($gamelogs_res) == 0 || !isset($this->game_data['gamelog']) || $this->action_time == 0 ){ $occuring = false; } // use game logs data if($occuring){ for($a = 0; $a <= $this->action_time; $a++){ if( isset($this->game_data['gamelog'][$a]) ){ $this_play_stats = json_decode($this->game_data['gamelog'][$a]['gamelog_stats'], true); if( !empty($this_play_stats) ){ foreach($this_play_stats as $pid => $player_datum){ foreach($player_datum as $stat => $value){ $pid_team_id = $pid_team_hash[$pid]; if( !isset($data[$pid_team_id][$pid][$stat]) ){ $data[$pid_team_id][$pid][$stat] = 0; } $data[$pid_team_id][$pid][$stat] += $value; } } } } } // use game data record } else { $game_stats = json_decode($this->game_data['game_stats'], true); foreach($game_stats as $pid => $player_datum){ $pid_team_id = $pid_team_hash[$pid]; $data[$pid_team_id][$pid] = $player_datum; } } return $data; }