Ryan
Ryan

Reputation: 985

Issue with foreach routine adding variable to array

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.

Upvotes: 0

Views: 72

Answers (2)

RbG
RbG

Reputation: 3193

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

Upvotes: 0

complex857
complex857

Reputation: 20753

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

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

You probably wanted to write only one

Upvotes: 1

Related Questions