douli4337 2013-07-06 14:36
浏览 39
已采纳

foreach例程向数组添加变量的问题

When calling the function below and passing the parameters "test/asdf" for $url and "variables" for $element, the foreach routine doesn't add the variable $var to the $variables array. However, the routine correctly prints out the $var value.

public function ParseURL($url, $element)
    {
        $splitURL = preg_split("/(\\/)/is", $url);
        if ($element == "page_id"){return $splitURL[0];}
        elseif ($element == "all"){return $splitURL;}
        elseif ($element == "variables"){
            $i = 1;
            $variables = array(0 => "test");
            foreach ($splitURL as $var) {
                if ($var != $splitURL[0]){
                    $variables[$i] == $var;
                    echo $var;
                    echo $variables[$i];
                    $i++;
                }
            }
            var_dump($variables);
            return $variables;
        }
    }

Edit

The input URL would be everything after the domain of a webpage, so if you inputted the string example.com/function/variable/variable2, it would return either "function", an array containing "function", variable and variable2 or an array containing "variable" and "variable" based on the string inputted into the element parameter.

The expected output is an array of variable and variable2.

  • 写回答

2条回答 默认 最新

  • doufu8887 2013-07-06 14:39
    关注

    You have a typo in the assignment, in your example, theres a double ==:

    $variables[$i] == $var;
                   ^^
    

    You probably wanted to write only one

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测