homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Array "Streaks"
Robert Poole

 10:12 am on Sep 17, 2008 (gmt 0)

At the moment I'm working on a basketball website and thought a neat feature would be to have the current "streak" that a team is on displayed on their page. For instance a "3 game winning streak" or a "2 game losing streak". Streaks can be only 1 game, that's fine.

So all of the results are in a database, so let's say I run through and add either "W" or "L" to an array for each one which for example would give me an array like

W, L, L, W, L, W, W, W

How would I then get this to tell me that the last three are wins so the team must be on a 3 game winning streak?



 10:41 am on Sep 17, 2008 (gmt 0)

Try this function on for size,

function getStreak($streak)
$count = 0;
while ($game = array_pop($streak))
if (!isset($type))
$type = $game;
if ($game == $type)
switch ($game)
case "W":
$type = "Win" . ($count != 1 ? "s" : "");
case "L":
$type = "Loss" . ($count != 1 ? "es" : "");
return NULL;
return $count . " " . $type;


 10:43 am on Sep 17, 2008 (gmt 0)

function streaks($inArray) {
$length = count($inArray);
$win = 0;
$loose = 0;
$lastResult = NULL;
for ($i=$length-1; $i>-1; $i--) { // count backwards
if ($lastResult === NULL $inArray[$i] == $lastResult) {
switch ($inArray[$i]) {
case 'W':
case 'L':
else {
$lastResult = $inArray[$i];
$win ? $out = "The winning streak is: $win" : $out = "The loosing streak is: $loose";
echo $out;
$test = array('W', 'W', 'L', 'L', 'L', 'W', 'W');

Could maybe do with an additional check to make sure that it is an array being send to the function. If not an array then you could use explode to make an array.

Beaten by 2 mins ;)

[edited by: PHP_Chimp at 10:45 am (utc) on Sep. 17, 2008]

Robert Poole

 11:29 am on Sep 17, 2008 (gmt 0)

Thanks for your quick responses!

I tried Sekka's approach and it worked fine and dandy... except it got wins and losses the wrong way round! So I just changed the case "L" to case "W" and vice versa and now it's perfecto.

Thanks again


 11:41 am on Sep 17, 2008 (gmt 0)

Balls! It's doesn't get them the wrong way round, there is an error in my code.


switch ($game)


switch ($type)


Robert Poole

 11:43 am on Sep 17, 2008 (gmt 0)

Okay, question number two!

Sorry, just thought of this, but I'm trying to build up a list of neato features!

Let's say we take the streak that PHP_Chimp suggested:
W, W, L, L, L, W, W

I was wondering if it would be possible to have a feature that would be like "Longest ever winning streak" and "Longest ever losing streak", so in this case it would be 2 wins and 3 losses if these were all the results ever. Bigger picture I guess it would have to cycle through the seasons like


for each season {
find longest winning streak
find longest losing streak
check to see if these streaks are longer than other seasons
if they are {
longest winning streak season = this season


So you could output like:

Longest Winning Streak: 5 wins (05/06 season)
Longest Losing Streak: 4 losses (02/03 season)

Is this possible? I'm kind of just brainstorming... not sure if it's possible to pull streaks out of a middle of an array?


 11:52 am on Sep 17, 2008 (gmt 0)

I'm bit busy at work now so I can't do a function.

However, what I will say is that the function for this could potentially do a largish DB call and then have to run through all the results.

If this is being displayed on a regular basis it could add load to the server. I would suggest working this out once and storing as part of the DB record, and whenever another result is added, update the figure in the DB record.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved