Jump to content

Display Missing Value in List Code


dlebowski

Recommended Posts

I am trying to figure out a way to have php give me the next "available" value, in a list.  This seems easy enough, but there may be some values that have a letter in them.  So for example, if I have this:

 

1

2

3

3a

5

5a

7

 

The next available value would be 4. 

 

If I have this:

 

1

2

3

3a

4

5

5a

7

 

The next available value would be 6.

 

I am using this code to sort list correctly, and it appears to be working fine:

 

$sql="select LotNumber FROM lots WHERE Date='$Date' ORDER BY CAST(LotNumber AS UNSIGNED) asc, LotNumber";

 

I just need to now be able to take that list, and display the next available numeric value.  Any help on this would be greatly appreciated.  Thank you in advance!

 

Ryan

Link to comment
https://forums.phpfreaks.com/topic/200515-display-missing-value-in-list-code/
Share on other sites

//assume you have array  as
$arr = array('1','2','3','3a','5','5a','6','7');

function toInt($n){
   return (int)$n;
}
$newArr = $arr;
array_walk($newArr,"toInt");
print_r($newArr);
$temp =1;
for($i=0;$i<count($newArr)+1;$i++)
{          
           if(!in_array($temp,$newArr)){
                echo "Aval:- ".$temp; 
                exit;
          }            
         ++$temp;
}

  • 2 months later...

I want to open this back up again because I have one more issue.  If I have 1, 2, 3, 3a, 3b, 3c and I want the next value after 3, which can't be 3a/3b/3c because they are already used, how would I do that?  Basically, if I want next value "available" after 3, I would want 3d.  Can this be done?  Let me know if I need to explain it a little better.  Thank you!

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.