慕村225694
好吧,在不理解的情况下,您的特定用例是什么,只是您想检查什么本质上等同于某种编码状态......我冒昧地展示了一个完全演示的用例。这个例子的核心是这个 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 }