迭代虽然数组没有找到任何值

时间:2017-01-07 00:11:07

标签: php arrays

我有一个客户端的小脚本,用于为输入名称创建数字命理结果。在我第一次遍历名称及其相应的值之后,我需要在最终结果中添加数字,并获得所谓的“MASTER NUMBER”。我遇到的问题是迭代最终结果,它会一直返回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>

1 个答案:

答案 0 :(得分: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”),因为“算法”不能没有,原因显而易见。对不起。但我怀疑这并不重要,是吗? ; - )