jdubwelch Posted December 27, 2006 Share Posted December 27, 2006 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? Quote Link to comment Share on other sites More sharing options...
Chronos Posted December 27, 2006 Share Posted December 27, 2006 Check out this fucntion, using this in combination with some sort function you should be able to make a good (and much simpler ;)) code![url=http://www.php.net/manual/en/function.array-count-values.php]array_count_values()[/url] Quote Link to comment Share on other sites More sharing options...
bljepp69 Posted December 27, 2006 Share Posted December 27, 2006 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.