canabatz Posted December 23, 2011 Share Posted December 23, 2011 i need to get two or more of id ranges : for example id value 1 1 2 1 3 0 - 4 0 - 5 0 - 6 1 7 1 8 0 - 9 0 - 10 0 - 11 1 12 1 13 1 i want to get the starting id 0f the value 0 to the Third value 0 so the result will be 3-5 and i need to get the second one also 8-9. what is the easiest way to do that? or where to start? thanks Quote Link to comment https://forums.phpfreaks.com/topic/253749-geting-min-id-and-max-id-in-loop/ Share on other sites More sharing options...
AGuyWithAthing Posted December 24, 2011 Share Posted December 24, 2011 Not sure exactly what you mean but the following should work: <?php $array = array( 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1 ); $rangeMatches = array(); $rangeFlag = 0; $rangeMatch = false; foreach( $array AS $key => $val ) { if( $val === $rangeFlag && !$rangeMatch ) { $rangeMatches[] = $key; $rangeMatch = true; } if( $val !== $rangeFlag && $rangeMatch ) { $rangeMatches[ sizeof( $rangeMatches ) - 1 ] .= sprintf( '-%d', $key ); $rangeMatch = false; } } foreach( $rangeMatches AS $key => $val ) printf( "Range %s\n", $val ); Not tested this just cobbled it together. Quote Link to comment https://forums.phpfreaks.com/topic/253749-geting-min-id-and-max-id-in-loop/#findComment-1301052 Share on other sites More sharing options...
canabatz Posted December 24, 2011 Author Share Posted December 24, 2011 Thanks AGuyWithAthing, im going to try that!! Quote Link to comment https://forums.phpfreaks.com/topic/253749-geting-min-id-and-max-id-in-loop/#findComment-1301130 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.