【问题标题】:Iterating though array is finding no values遍历数组是找不到值
【发布时间】:2017-01-07 00:11:07
【问题描述】:

我有一个用于客户端的小脚本,旨在为输入名称创建命理结果。在对名称及其对应值进行第一次迭代后,我需要在最终结果中添加数字,并获得所谓的“主编号”。我遇到的问题是迭代最终结果,它一直返回 0 - 沿着迭代检查表明它没有在数组中找到值。我哪里出错了?

<?php
if($_POST) 
 {
     // create an array based on chaldean numerology

     $ar = array('A' => 1,
         'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,
         'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,
         'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,
         'Z' => 8,'1' => 1,'2' => 2,'3' => 3,'4' => 4,'5' => 5,'6' => 6,'7' => 7,
         '8' => 8,'9' => 9);

     //get the value entered by post method
     $fname = $_POST['fname'];
     //make it upper case. to avoid messing with small letters.
     $fname = strtoupper($fname);
     //find the length of the string entered
     $len = strlen($fname);

     //set a temp value to calculate
     $fullnum = 0;
     $masternum = 0;

     //now loop through the string one by one and add the values
     for($i=0; $i<$len; $i++)
     {
         $alpha  = $fname[$i];
         $fullnum = $ar[$alpha] + $fullnum;
     }


     //now loop through the fullnum one by one and add the values
     $flen = strlen($fullnum);

     for($i=0; $i<$flen; $i++)
     {
         $alpha  = $fullnum[$i];
         $masternum = $ar[$alpha] + $masternum;
     }

     //print the result
     //echo "FLEN = " . $flen . "<br>";
     echo "<hr>INTEGRATED SELF NUMBER: ". $fullnum . "/" . $masternum;
}

?>

<form action="" method="post">
     <input type="text" name="fname"  value="Full Name" />
     <input type="submit" value="calculate" />
</form>

【问题讨论】:

  • 这太可怕了......我几天前对这个问题发表了一些评论。现在我回来了,我看到评论消失了。我没有收到任何关于评论被删除或任何事情的通知。发生了什么?那条评论在哪里?
  • 这个问题实际上有 6 个来自三个用户的问题。显然他们全部都被删除了。这在我的胃里留下了一种非常不好的感觉。这种形式的审查不应该在这里引入 SO。 尤其是不留痕迹。
  • 我实际上只看到过一条评论——这令人不安……
  • 我原来的评论有两条回复,确实是在开玩笑。

标签: php arrays


【解决方案1】:

您在该“算法”中有两个问题:1. 您没有在该字典数组中定义键“0”,以及 2. 您必须将 $fullnum 的数字类型转换为字符串才能访问尝试使用数组表示法中的字符。

<?php
if($_POST) {
    // create an array based on chaldean numerology    
    $ar = [
        'A' => 1,
        'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,
        'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,
        'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,
        'Z' => 8,'0' => 0,'1' => 1,'2' => 2,'3' => 3,'4' => 4,'5' => 5,'6' => 6,
        '7' => 7,'8' => 8,'9' => 9
    ];

    //get the value entered by post method
    $fname = $_POST['fname'];
    //make it upper case. to avoid messing with small letters.
    $fname = strtoupper($fname);
    //find the length of the string entered
    $len = strlen($fname);

    //set a temp value to calculate
    $fullnum = 0;
    $masternum = 0;

    //now loop through the string one by one and add the values
    for($i=0; $i<$len; $i++) {
        $alpha  = $fname[$i];
        $fullnum = $ar[$alpha] + $fullnum;
    }

    //now loop through the fullnum one by one and add the values
    $fullnum = "$fullnum";
    $flen = strlen($fullnum);
    for($i=0; $i<$flen; $i++) {
        $alpha  = $fullnum[$i];
        $masternum = $ar[$alpha] + $masternum;
    }

    //print the result
    //echo "FLEN = " . $flen . "<br>";
    echo "<hr>INTEGRATED SELF NUMBER: ". $fullnum . "/" . $masternum;
}
?>
<form action="" method="post">
<input type="text" name="fname"  value="Full Name" />
<input type="submit" value="calculate" />
</form>

稍作修改的版本确实创建了一个输出。但是我当然不知道这是否是正确的神秘数字,因为我不得不更改魔法字典并添加一个明显缺失的值(“0”),因为显而易见的原因,没有“算法”就无法工作。对此感到抱歉。但我怀疑这并不重要,不是吗? ;-)

【讨论】:

  • 完全做到了——看起来最大的罪魁祸首是我的变量被转换为整数,而不是字符串。谢谢 - 我犯了一个愚蠢的错误。
猜你喜欢
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 2014-11-29
  • 2018-08-22
  • 2023-03-27
  • 2014-10-28
  • 2021-04-08
相关资源
最近更新 更多