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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧