Home > Blockchain >  How to get a missing string sequence in PHP Laravel
How to get a missing string sequence in PHP Laravel

Time:09-12

we have warehouse LOCATIONS in a system like this.

D2705B, D2805C, F3805C, F3805F

So we used these all locations but want to know which locations are not used YET.

Case 1: D2705B USED means missing D2705A (Need to go back so ending with A is the missing location)

Case 1:D2805C USED means missing D2805B, D2805A (Need to go back so ending with A and B is the missing location)

Case 3: F3805C, F3805F First 4 digits same so C and F are used but B, A and D is missing

Basic Requirments are: For example, we have a stock location in system D2705A, D2705E, but if no product is using D2705B, C, D, then the system should show missing location D2705B, C, D;

CodePudding user response:

As far as I understand your question, you need to generate all missing "locations" based on the last letter from the number. So if you have: F3805F, you need to get F3805A, F3805B, F3805C, F3805D, F3805E.

$location = 'F3805F'; // last location 
$base = substr($location, 0, 5); // gives 'F3805'
$lastLetter = substr($location, -1); // gives 'F'

$missingLocations = [];

foreach (range('A', $lastLetter)as $letter) {
  if ($letter === $lastLetter) {
    break;
  }
  $missingLocations[] = $base.$letter;
}

print_r($missingLocations);

That gives you an array of missing locations:

Array ( [0] => F3805A [1] => F3805B [2] => F3805C [3] => F3805D [4] => F3805E )

You can now diff this array with existing locations.

CodePudding user response:

$locations = ['Y1103A' , 'Y1103C']; #it will return me Y1103C
$locations = ['Y1103M']; #it will return me Y1103A to Y1103N

 if ($locations) {

    $first = strtoupper(current($locations));
    $last = strtoupper(end($locations));

    if (count($locations) == 1)
       $first = 'A';

        foreach (range($first, $last) as $letter) {
         if ($letter === $last) {
            break;
         }
         if (in_array($letter, $locations)) continue;
             $foundMissingLocations[] = $loc->stock_location_4 . $letter;
         }

   print_r($foundMissingLocations);
 }
  • Related