dongyin5516 2018-07-17 10:35
浏览 74
已采纳

比较增量字符串

so this is kinda hard to form a question about my issue so just picture what i'm going to explain,

image you have a list of multiple strings that contain characters from A to Z after Z it goes to AA, AB, AC etc etc...

I want to get the highest on the list for that i'm trying to do something like this:

$max_letter = "A";
while($row = mysqli_fetch_assoc($recordset)) {
    if($row["new_letter"] > $max_letter) {
        $max_letter = $row["new_letter"];
    }
}

the problem with this is that when $row["new_letter"] is equal to "AA" and $max_letter is equal to "Z" it doesn't update $max_letter to "AA" because it thinks "Z" is higher than "AA"

but if i do something like this:

$str = "Z";
echo ++$str;

my output will be

AA

example of my problem:

$str = "Z";
echo ++$str;

echo "<br><br>";

if("AA" > "Z") {
    echo "higher";
}elseif("AA" == "Z") {
    echo "equal";   
}elseif("AA" < "Z") {
    echo "lower";
}
  • 写回答

3条回答 默认 最新

  • douyiqi9640 2018-07-17 11:00
    关注

    this would likely work:

    $max_letter = "A";
    while($row = mysqli_fetch_assoc($recordset)) {
        if(cvrt($row["new_letter"]) > cvrt($max_letter)) {
            $max_letter = $row["new_letter"];
        }
    }
    
    function cvrt(incStr){
        $ret = "";
        foreach(str_split(incStr) as $v){
            $ret .= ord($v);
        }
        return $ret;
    }
    

    not pretty but I think it will work for any length of string.

    what you are doing here is concatenating the numerical values of the letters in the string and returning them to be compared as a number.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog