Jump to content

Geting min id and max id in loop


canabatz

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/253749-geting-min-id-and-max-id-in-loop/
Share on other sites

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.

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.