Jump to content

help, finding consective elements in an array


jdubwelch

Recommended Posts

i have an array:

[code]Array $streak:
(
    [0] => W
    [1] => W
    [2] => L
    [3] => W
    [4] => L
    [5] => L
    [6] => W
    [7] => L
)[/code]

I need to find the winning or losing streak from this and other arrays like it. $streak[0] is the last game that was played.  This would be a 2 Game Winning Streak. 

I just can't put my brain around how i'd go through and figure it out. 

this is what i've tried, but it's just returning the number of ones that match up.

[code] // GET THEIR STREAK
$streak = get_streak ($gid, $row[user_id]); // this returns the array you see above

$streaklength = 1;

$n = count($streak);

for ($i = 1; $i < $n; $i++) {
if ($streak[0] == $streak[$i]) {
$streaklength++;
} else {  // i want to exit the for loop and stop but i don't know how
}
}[/code]

ideas?
You should be able to just add a 'break' statement:

[code]
<?php
       for ($i = 1; $i < $n; $i++) {
               if ($streak[0] == $streak[$i]) {
                       $streaklength++;
               } else {  // i want to exit the for loop and stop but i don't know how
                       break;
               }
       }

?>
[/code]

Then check the value of $streaklength

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.