0

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.

3
  • What does the URL look like ? What is your expected output ... you need to add this details .... Commented Jul 6, 2013 at 14:45
  • Sorry, added it onto the question for future reference. Commented Jul 6, 2013 at 14:50
  • Just use explode and parse_url Commented Jul 6, 2013 at 15:01

2 Answers 2

1

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

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

You probably wanted to write only one

Sign up to request clarification or add additional context in comments.

Comments

0

change $variables[$i] == $var; to $variables[$i] = $var;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.