Php array to pow numbers reverses 函数问题

Php array to pow numbers revers function problem

<style>
.red{color:red;}
.green{color:green;}
</style>
<?php
function array2aiflag($inputString){
    $arAIFlag = ["AGGR","NOMOVE","COWARD","NOATTSHINSU","NOATTCHUNJO","NOATTJINNO","ATTMOB","BERSERK","STONESKIN","GODSPEED","DEATHBLOW","REVIVE"];
    $retValue = 0;
    $arInputString = explode(",",$inputString);
    for($i=0;$i<sizeof($arAIFlag);$i++) {
        $tempString = $arAIFlag[$i];
        for ($j=0;$j<30;$j++){
            if(isset($arInputString[$j])){
                    $tempString2 = $arInputString[$j];
            }else{
                $tempString2 = $inputString;
            }
            if(strcmp($tempString2,$tempString) == 0){
                $retValue+=pow(2,$i);
            }           
            if(strcmp($tempString2,"") == 0){
                break;
            }
        }
    }
    unset($arInputString);
    return $retValue;
}
function aiflag2array($flag,$target=[],$str=""){
    $tar=["AGGR","NOMOVE","COWARD","NOATTSHINSU","NOATTCHUNJO","NOATTJINNO","ATTMOB","BERSERK","STONESKIN","GODSPEED","DEATHBLOW","REVIVE"];
    for($x=0;$x<count($tar);$x++){
        $target[pow(2,$x)]=$tar[$x];
    }   
    for($i=count($tar)-1;$i>-1;$i--){
        $pow = pow(2, $i);
        if($pow < $flag){
            $str = ",".$target[$pow].$str;
            $flag = $flag-$pow;
        }elseif($pow == $flag){
            return "".$target[$pow].$str;
        }
    }
    return "NONE";
}


$sample=["AGGR","AGGR,BERSERK","AGGR,BERSERK,DEATHBLOW,REVIVE","AGGR,BERSERK,STONESKIN","AGGR,BERSERK,STONESKIN,DEATHBLOW","AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE","AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE","AGGR,DEATHBLOW","AGGR,GODSPEED","AGGR,GODSPEED","AGGR,NOATTCHUNJO","AGGR,NOATTJINNO","AGGR,NOATTSHINSU","AGGR,NOMOVE","AGGR,REVIVE","AGGR,STONESKIN","BERSERK","BERSERK,STONESKIN","COWARD","NOMOVE","AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW","NOMOVE,STONESKIN","NOMOVE,STONESKIN","REVIVE"];
foreach($sample as $k=>$v){
    $no1=array2aiflag($v);
    $no2=array2aiflag(aiflag2array($no1));
    if($v != aiflag2array($no1)){
        echo '<a class="red"> OriginalData - <b>'.$v.' ['.$no1.']</b> - ReversData - <b>'.aiflag2array($no1).' ['.$no2.']</b><br>';
    }else{      
        echo '<a class="green"> OriginalData - <b>'.$v.' ['.$no1.']</b> - ReversData - <b>'.aiflag2array($no1).' ['.$no2.']</b><br>';
    }
}
?>

当我运行上面的代码时,我将24个样本数据转换成一个折叠数字并打印出来。然后当我想从数据库中调用数字并将它们转换回字符串时,其中一些似乎是错误的。

结果:

OriginalData - AGGR [30] - ReversData - NOMOVE,COWARD,NOATTSHINSU,NOATTCHUNJO [30]
OriginalData - AGGR,BERSERK [129] - ReversData - AGGR,BERSERK [129]
OriginalData - AGGR,BERSERK,DEATHBLOW,REVIVE [3201] - ReversData - AGGR,BERSERK,DEATHBLOW,REVIVE [3201]
OriginalData - AGGR,BERSERK,STONESKIN [385] - ReversData - AGGR,BERSERK,STONESKIN [385]
OriginalData - AGGR,BERSERK,STONESKIN,DEATHBLOW [1409] - ReversData - AGGR,BERSERK,STONESKIN,DEATHBLOW [1409]
OriginalData - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457] - ReversData - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457]
OriginalData - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457] - ReversData - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457]
OriginalData - AGGR,DEATHBLOW [1025] - ReversData - AGGR,DEATHBLOW [1025]
OriginalData - AGGR,GODSPEED [513] - ReversData - AGGR,GODSPEED [513]
OriginalData - AGGR,GODSPEED [513] - ReversData - AGGR,GODSPEED [513]
OriginalData - AGGR,NOATTCHUNJO [17] - ReversData - AGGR,NOATTCHUNJO [17]
OriginalData - AGGR,NOATTJINNO [33] - ReversData - AGGR,NOATTJINNO [33]
OriginalData - AGGR,NOATTSHINSU [9] - ReversData - AGGR,NOATTSHINSU [9]
OriginalData - AGGR,NOMOVE [3] - ReversData - AGGR,NOMOVE [3]
OriginalData - AGGR,REVIVE [2049] - ReversData - AGGR,REVIVE [2049]
OriginalData - AGGR,STONESKIN [257] - ReversData - AGGR,STONESKIN [257]
OriginalData - BERSERK [3840] - ReversData - STONESKIN,GODSPEED,DEATHBLOW,REVIVE [3840]
OriginalData - BERSERK,STONESKIN [384] - ReversData - BERSERK,STONESKIN [384]
OriginalData - COWARD [120] - ReversData - NOATTSHINSU,NOATTCHUNJO,NOATTJINNO,ATTMOB [120]
OriginalData - NOMOVE [60] - ReversData - COWARD,NOATTSHINSU,NOATTCHUNJO,NOATTJINNO [60]
OriginalData - AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW [1667] - ReversData - AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW [1667]
OriginalData - NOMOVE,STONESKIN [258] - ReversData - NOMOVE,STONESKIN [258]
OriginalData - NOMOVE,STONESKIN [258] - ReversData - NOMOVE,STONESKIN [258]
OriginalData - REVIVE [61440] - ReversData - NONE [0]

需要进行逆向工程。 aiflag2array函数中的数字30有问题

不应修改 array2aiflag 函数。 aiflag2数组需要编辑。

如果你能帮我解决这个问题,我将不胜感激。谢谢。

好的,我认为这是一件值得继续努力的好事。我必须说我错过了上下文,我不知道这将用于什么。上下文对于理解代码很重要。我猜这是某种游戏?

首先要做的是把数组定义移开:

$states = ["AGGR",
           "NOMOVE",
           "COWARD",
           "NOATTSHINSU",
            "NOATTCHUNJO",
           "NOATTJINNO",
           "ATTMOB",
           "BERSERK",
           "STONESKIN",
           "GODSPEED",
           "DEATHBLOW",
           "REVIVE"];

$samples = ["AGGR",
            "AGGR,BERSERK",
            "AGGR,BERSERK,DEATHBLOW,REVIVE",
            "AGGR,BERSERK,STONESKIN",
            "AGGR,BERSERK,STONESKIN,DEATHBLOW",
            "AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE",
            "AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE",
            "AGGR,DEATHBLOW",
            "AGGR,GODSPEED",
            "AGGR,GODSPEED",
            "AGGR,NOATTCHUNJO",
            "AGGR,NOATTJINNO",
            "AGGR,NOATTSHINSU",
            "AGGR,NOMOVE",
            "AGGR,REVIVE",
            "AGGR,STONESKIN",
            "BERSERK",
            "BERSERK,STONESKIN",
            "COWARD",
            "NOMOVE",
            "AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW",
            "NOMOVE,STONESKIN",
            "NOMOVE,STONESKIN",
            "REVIVE"];

我用了$states$samples。当然,您可以更改这些名称。重要的是你的行不要太长,不要超过 80 - 100 个字符。

接下来是从我称之为 flagsstates 的转换代码,反之亦然:

function states2flags($userStates, $allStates)
{
    $userFlags = 0;
    $bit = 1;
    foreach ($allStates as $state) {
        if (in_array($state, $userStates)) {
           $userFlags += $bit;
        }
        $bit <<= 1;
    }
    return $userFlags;
}

function flags2states($userFlags, $allStates)
{
    $userStates = [];
    $bit = 1;
    foreach ($allStates as $state) {
        if ($userFlags & $bit) {
           $userStates[] = $state;
        }
        $bit <<= 1;
    }
    return $userStates;
}

这里唯一特别的是Bitwise Assignment Operator。使用 2 的幂不需要 pow()

每个函数参数都是一个数组,这是故意的。它显然是一个数组,那么为什么要使用字符串呢?或者,您可以选择仅使用字符串,但不要混合使用数组和字符串,这很混乱。

我保留了您的大部分测试代码,尽管名称已更改:

foreach($samples as $sample) {
    $sampleStates  = explode(",", $sample);
    $sampleFlags   = states2flags($sampleStates, $states);
    $reverseStates = flags2states($sampleFlags, $states);
    $reverseFlags  = states2flags($reverseStates, $states);
    $textColor = (count(array_diff($sampleStates, $reverseStates)) > 0) ? "red" : "green";
    echo '<a class="' . $textColor . '">' . $textColor . ' Original Data - <b>' . $sample .
         ' [' . $sampleFlags . ']</b> - Reverse Data - <b>' .
         implode(",", flags2states($sampleFlags, $states)) . ' ['.$reverseFlags. ']</b><br>';
}

输出为:

green Original Data - AGGR [1] - Reverse Data - AGGR [1]
green Original Data - AGGR,BERSERK [129] - Reverse Data - AGGR,BERSERK [129]
green Original Data - AGGR,BERSERK,DEATHBLOW,REVIVE [3201] - Reverse Data - AGGR,BERSERK,DEATHBLOW,REVIVE [3201]
green Original Data - AGGR,BERSERK,STONESKIN [385] - Reverse Data - AGGR,BERSERK,STONESKIN [385]
green Original Data - AGGR,BERSERK,STONESKIN,DEATHBLOW [1409] - Reverse Data - AGGR,BERSERK,STONESKIN,DEATHBLOW [1409]
green Original Data - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457] - Reverse Data - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457]
green Original Data - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457] - Reverse Data - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457]
green Original Data - AGGR,DEATHBLOW [1025] - Reverse Data - AGGR,DEATHBLOW [1025]
green Original Data - AGGR,GODSPEED [513] - Reverse Data - AGGR,GODSPEED [513]
green Original Data - AGGR,GODSPEED [513] - Reverse Data - AGGR,GODSPEED [513]
green Original Data - AGGR,NOATTCHUNJO [17] - Reverse Data - AGGR,NOATTCHUNJO [17]
green Original Data - AGGR,NOATTJINNO [33] - Reverse Data - AGGR,NOATTJINNO [33]
green Original Data - AGGR,NOATTSHINSU [9] - Reverse Data - AGGR,NOATTSHINSU [9]
green Original Data - AGGR,NOMOVE [3] - Reverse Data - AGGR,NOMOVE [3]
green Original Data - AGGR,REVIVE [2049] - Reverse Data - AGGR,REVIVE [2049]
green Original Data - AGGR,STONESKIN [257] - Reverse Data - AGGR,STONESKIN [257]
green Original Data - BERSERK [128] - Reverse Data - BERSERK [128]
green Original Data - BERSERK,STONESKIN [384] - Reverse Data - BERSERK,STONESKIN [384]
green Original Data - COWARD [4] - Reverse Data - COWARD [4]
green Original Data - NOMOVE [2] - Reverse Data - NOMOVE [2]
green Original Data - AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW [1667] - Reverse Data - AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW [1667]
green Original Data - NOMOVE,STONESKIN [258] - Reverse Data - NOMOVE,STONESKIN [258]
green Original Data - NOMOVE,STONESKIN [258] - Reverse Data - NOMOVE,STONESKIN [258]
green Original Data - REVIVE [2048] - Reverse Data - REVIVE [2048]