dongxiane0395 2013-03-29 20:35
浏览 29
已采纳

PHP中foreach循环中的变量[关闭]

I have never been good at using loops. I have the following problem:

When I execute the code below, I get this error message: "Parse error: syntax error, unexpected T_VARIABLE in XXXXXXXXX on line 8". I just want to give each of the elements in the array a "rating" on how high the value is.

    <?
    $input = array( 12413535, 13452465246, -13451, 8);
    $input_size = count($input);
    $var_rating = array ();
    foreach ($input as $value) {
        $var_rating[$value] = 0;
        foreach ($input as $test) {
            if ($value > $test) {
                $var_rating[$value] = $var_rating[$value] + 1;
            }
            else {}
        }
    }

    var_dump($var_rating);
?>

Sorry for the confusion. I edited my question and added the full code.

  • 写回答

3条回答 默认 最新

  • dongyue5686 2013-03-29 21:02
    关注

    It would appear the culprit to your code not working is a copied hidden hidden character on line 8, just before the $test variable. Deleting the 'space' and the > sign, and retyping both fixes your issue on my end.

    <?php
        $input = array( 12413535, 13452465246, -13451, 8);
        $input_size = count($input);
        $var_rating = array ();
        foreach ($input as $value) {
            $var_rating[$value] = 0;
            foreach ($input as $test) {
                if ($value > $test) {
                         // ^  right here there is a hidden character, remove it and the code works fine
                    $var_rating[$value] = $var_rating[$value] + 1;
                }
                else {}
            }
        }
    
        var_dump($var_rating);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?