[英]How to get a missing string sequence in PHP Laravel
我們在這樣的系統中有倉庫位置。
D2705B、D2805C、F3805C、F3805F
所以我們使用了所有這些位置,但想知道哪些位置尚未使用。
案例 1:D2705B USED 表示缺少 D2705A(需要返回 go,所以以 A 結尾是缺少的位置)
案例 1:D2805C USED 表示缺少 D2805B、D2805A (需要 go 后面所以以 A 和 B 結尾是缺少的位置)
案例 3:F3805C、F3805F 前 4 位相同,因此使用了 C 和 F,但缺少 B、A 和 D
基本要求是:例如我們在系統D2705A,D2705E中有一個庫存位置,但是如果沒有產品使用D2705B,C,D,那么系統應該顯示缺少位置D2705B,C,D;
據我了解您的問題,您需要根據數字中的最后一個字母生成所有缺失的“位置”。 所以如果你有: F3805F
,你需要得到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);
這為您提供了一系列缺失的位置:
數組([0] => F3805A [1] => F3805B [2] => F3805C [3] => F3805D [4] => F3805E)
您現在可以將此數組與現有位置進行比較。
$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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.