Jump to content

Finding Multiples in an Array and Combining Them


Waizujin

Recommended Posts

Okay I have a script that takes a server log of a players kills at the end of a game in Halo Custom Edition. This script will parse the log grabbing players with a certain value in their name (in this case, my clan tag) and display their name and the kills from each of their games. What I am needing help with is, how do I find multiple entries of the same name and combine the scores making one total score.

Here is my script that parses the log file.

<html>
<head>
	<title>Halo Custom Edition BC Kill Log</title>
</head>
<body>
	<?php
		$lines = file('halo.log');
		$teams = array();
		foreach($lines as $line) {
			if(preg_match('/PCR_PLAYER/',$line)) {
				$teams[] = $line;
			}
		}

		$teamnames = array();
		$teamscore = array();

		foreach($teams as $team) {
			$teamarr = split(chr(9),$team);
			if(trim($teamarr[1]) == 'PCR_PLAYER' && preg_match("/»ßÇ«/", $teamarr[3], $matches)) {
				$teamnames[] = $teamarr[3];
				$teamnames = preg_replace("/Player [0-9]*/", "", $teamnames);
				$teamnames = str_replace('"', '', $teamnames);
				$teamscore[] = $teamarr[5];
			}
		}
	?>

	<table>
		<tr>
			<th>team</th>
			<th>score</th>
		</tr>
		<?php
			for($i=0;$i<count($teamnames);$i++) {
				echo '<tr><td>'.$teamnames[$i].'</td><td>'.$teamscore[$i].'</td></tr>';
			}
		?>
	</table>
</body>
</html>

 

Here is a small bit of the log file that this script trys to find. (The actual log file is 10mb's)

2010-03-03 22:30:29 PCR_PLAYER Place 1 Player 4 "Itachi kun" Team 0 Kills 27 Assists 0 Deaths 15 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 2 Player 12 "lukitas" Team 1 Kills 20 Assists 0 Deaths 23 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 3 Player 7 "freddy" Team 1 Kills 19 Assists 0 Deaths 13 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 4 Player 14 "Iwuana" Team 1 Kills 16 Assists 0 Deaths 34 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 5 Player 1 "DARKWARRIOR" Team 0 Kills 12 Assists 0 Deaths 15 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 6 Player 8 "jhon" Team 0 Kills 11 Assists 0 Deaths 11 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 7 Player 15 "New001" Team 0 Kills 11 Assists 1 Deaths 19 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 8 Player 5 "GES=Terry" Team 0 Kills 9 Assists 0 Deaths 5 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 9 Player 10 "-KLBöòLL-" Team 0 Kills 6 Assists 0 Deaths 2 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 10 Player 9 "SAMIR" Team 0 Kills 5 Assists 0 Deaths 4 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 11 Player 6 "xxxmen" Team 1 Kills 4 Assists 0 Deaths 5 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 12 Player 3 "Weasel" Team 1 Kills 1 Assists 0 Deaths 29 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 13 Player 13 "DIEGO" Team 0 Kills 0 Assists 0 Deaths 0 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 13 Player 16 "elprimoluka" Team 1 Kills 0 Assists 0 Deaths 0 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 15 Player 2 "PENETREITOR" Team 1 Kills 0 Assists 0 Deaths 2 Score 0

2010-03-03 22:30:29 PCR_PLAYER Place 16 Player 11 "BONO" Team 1 Kills 0 Assists 0 Deaths 4 Score 0

This outputs the time, the players ranking, the player number, player name, player team, player kills, player assists and player deaths at the end of a game.

There are hundreds of these in a file. Say the player on there named "BONO" plays 5 games. I will have 5 separate entries with 5 unique values. I want to know how to combine those 5 separate entries into one number for the player "BONO".

My ultimate goal is to grab each of my clan members wearing my clan tag and grab kill totals and death totals to provide a proper ratio. However, for now I just need help combining the scores of people with the same name.

 

I hope I explained this properly, it is difficult for me to word things.

 

Also, here is part of the output when I run that script:

»ßÇ« Þå®å Kills 23

»ßÇ« Þå®å Kills 13

»ßÇ« Þå®å Kills 70

»ßÇ« Waiz Kills 0

»ßÇ« Waiz Kills 0

»ßÇ« TÊÃÃÂ¥ Kills 0

»ßÇ« Waiz Kills 0

»ßÇ« Waiz Kills 0

»ßÇ« Þå®å Kills 8

»ßÇ« Havoc Kills 4

»ßÇ« TÊÃÃÂ¥ Kills 4

»ßÇ« Waiz Kills 0

»ßÇ« Þå®å Kills 0

»ßÇ« Havoc Kills 0

»ßÇ« TÊÃÃÂ¥ Kills 0

»ßÇ« Waiz Kills 0

»ßÇ«l s1K l Kills 0

»ßÇ«l s1K l Kills 15

»ßÇ« Waiz Kills 14

»ßÇ« Havoc Kills 12

 

As you can see I have multiple entries for the same name. I want to combine the kill count. Thank you VERY much for your help :)

Link to comment
Share on other sites

try

<html>
<head>
	<title>Halo Custom Edition BC Kill Log</title>
</head>
<body>
	<?php
		$lines = file('halo.log');
		$teams = array();
		foreach($lines as $line) {
			if(preg_match('/PCR_PLAYER/',$line)) {
				$teams[] = $line;
			}
		}

		//$teamnames = array();
		$teamscore = array();

		foreach($teams as $team) {
			$teamarr = split(chr(9),$team);
			if(trim($teamarr[1]) == 'PCR_PLAYER' && preg_match("/»ßÇ«/", $teamarr[3], $matches)) {
				$name = $teamarr[3];
				$name = preg_replace("/Player [0-9]*/", "", $name);
				$name = str_replace('"', '', $name);
				if (isset($teamscore[$name])) $teamscore[$name] += $teamarr[5]; else $teamscore[$name] = $teamarr[5];
			}
		}
	?>

	<table>
		<tr>
			<th>team</th>
			<th>score</th>
		</tr>
		<?php
			foreach($teamscore as $name => $score) {
				echo '<tr><td>'.$name.'</td><td>'.$score.'</td></tr>';
			}
		?>
	</table>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.