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条)

报告相同问题?

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数