3 回答

TA貢獻1811條經驗 獲得超4個贊
我正在嘗試想一個更靈活的方法,但現在;您可以將變量組合成一個字符串并檢查:
switch($a.$b.$c.$d.$e) {
case '11111':
echo 'case_1';
break;
case '00000':
echo 'case_2';
break;
//etc...
}
至于您的編輯,只需在以下情況下使用變量:
case $none.$none.$none.$none.$none:
//or
case $none.'1111':
//etc

TA貢獻1813條經驗 獲得超2個贊
目前,我只能告訴你匹配表達式的存在,但僅在 PHP8 中可用:
$case = match($a.$b.$c.$d.$e) {
? ? '00000' => 'case_0',
? ? '11111' => 'case_1',
? ? ...
? ? // default => something,
};
這只是一個“小”switch

TA貢獻1880條經驗 獲得超4個贊
好吧,在不理解的情況下,您的特定用例是什么,只是您想檢查什么本質上等同于某種編碼狀態......我冒昧地展示了一個完全演示的用例。
這個例子的核心是這個 parseCrumbs 函數,它的抽象是這樣的......
function parseCrumbs($crumbs, $tranformer, $text = '') {
// provide a list of methods, to carry out some sort of tranformation, based on positions
static $places = ['negative', 'thousands', 'hundreds', 'tens', 'ones'];
if(is_string($crumbs))
$crumbs = str_split($crumbs, 1);
// Reduce $crumns through transformations...
foreach ( array_combine( $places, $crumbs ) as $method => $value ){
if($value) $text .= " " . $tranformer::$method($value, $text);
}
return $text === '' ? '-- empty --' : $text . PHP_EOL;
}
模擬用例演示:將 5 位置狀態字符串轉換為表示單詞總數...
class Transformations {
static public function ones ($count, &$text ) {
if($count)
return ($text === '' || $text !== 'Minus ') ? "( {$count} ) Ones" : "and ( {$count} ) Ones";
return '';
}
static public function tens ($count, &$text ) {
if($count)
return "( {$count} ) Tens";
return '';
}
static public function hundreds ($count, &$text ) {
if($count)
return "( {$count} ) Hundreds";
return '';
}
static public function thousands ($count, &$text ) {
if($count)
return "( {$count} ) Thousands";
return '';
}
static public function negative ($count, &$text ) {
if($count)
return 'Minus';
return '';
}
}
function parseCrumbs($crumbs, $translations, $text = '') {
static $places = ['negative', 'thousands', 'hundreds', 'tens', 'ones'];
if(is_string($crumbs))
$crumbs = str_split($crumbs, 1);
foreach ( array_combine($places, $crumbs ) as $method => $value ){
if($value) $text .= " " . $translations::$method($value, $text);
}
return $text === '' ? '-- you need money --' : $text . PHP_EOL;
}
// These examples use a String to encode the state,
// but the parseCrumbs function can also accept an array...
echo parseCrumbs('00001', Transformations::class); // ( 1 ) Ones
echo parseCrumbs('00011', Transformations::class); // ( 1 ) Tens ( 1 ) Ones
echo parseCrumbs('00111', Transformations::class); // ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones
echo parseCrumbs('00111', Transformations::class); // ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones
echo parseCrumbs('01111', Transformations::class); // ( 1 ) Thousands ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones
echo parseCrumbs('10001', Transformations::class); // Minus ( 1 ) Ones
echo parseCrumbs('10011', Transformations::class); // Minus ( 1 ) Tens ( 1 ) Ones
echo parseCrumbs('10111', Transformations::class); // Minus ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones
echo parseCrumbs('10111', Transformations::class); // Minus ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones
echo parseCrumbs('11111', Transformations::class); // Minus ( 1 ) Thousands ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones
為了演示排列...
for( $i = 5000; $i > 0; $i--)
echo parseCrumbs(str_pad($i, 5, "0", STR_PAD_LEFT), Transformations::class);
// ( 5 ) Thousands
// ( 4 ) Thousands ( 9 ) Hundreds ( 9 ) Tens and ( 9 ) Ones
// ( 4 ) Thousands ( 9 ) Hundreds ( 9 ) Tens and ( 8 ) Ones
// ... 4996 other permutations
// ( 1 ) Ones
當按照上面的方式建模時,大約有 49 行代碼,具體取決于您的轉換...您擁有處理 5000 種狀態的邏輯...
顯然,您的用例需要自定義,但“邏輯”可能會保持簡單明了。
在查看了上面的評論后,我整理了一個示例,其中包含類似于復選框類型輸入的數組......
class ShirtOptions {
public function __construct(){
$this->options = (object)[];
}
public function x_large ( $state ) {
if ($state) $this->options->shirt_size = "x_large";
}
public function xx_large ( $state ) {
if ($state) $this->options->shirt_size = "xx_large";
}
public function xxx_large ( $state ) {
if ($state) $this->options->shirt_size = "xxx_large";
}
public function small ( $state ) {
if ($state) $this->options->shirt_size = "small";
}
public function customPrinting ( $state ) {
if($state){
$this->options->custom_printing = true;
} else {
$this->options->custom_printing = false;
}
}
public function templateID ( $state ) {
if($state){
$this->options->template_id = $state;
}
}
public function parseOptions( $params ){
static $options = [ "x_large", "xx_large", "xxx_large", "small", "customPrinting", "templateID" ];
if(is_string($params))
$params = str_split($params, 1);
foreach ( array_combine( $options, $params ) as $option => $value )
if($value) $this->$option($value);
return $this;
}
public function to_json(){
return json_encode($this->options, JSON_PRETTY_PRINT);
}
}
echo (new ShirtOptions())->parseOptions([1,0,0,0,1,475])->to_json();
{
"shirt_size": "x_large",
"custom_printing": true,
"template_id": 475
}
- 3 回答
- 0 關注
- 212 瀏覽
添加回答
舉報