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? Link to comment https://forums.phpfreaks.com/topic/31939-help-finding-consective-elements-in-an-array/ 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] Link to comment https://forums.phpfreaks.com/topic/31939-help-finding-consective-elements-in-an-array/#findComment-148211 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 Link to comment https://forums.phpfreaks.com/topic/31939-help-finding-consective-elements-in-an-array/#findComment-148214 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.