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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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