2

I'm passing an array via ajax to a php.

$.post("send.php",{arr:arr}); //["one", "two", "three"]

How do I assign each value to an variable in php? I tried this but does not do what I need.

for ($i = 0; $i < $_POST['arr']; $i++){
   $var+($i+1) = $_POST['arr'][$i];
}

expected $var1 = "one", $var2 = "two",etc...

1 Answer 1

5

You are trying to create a dynamic variable name. You'll have to wrap the $var with {} and concatinate with . to create dynamic variables.

 ${"var" . ($i+1)} = $_POST['arr'][$i];
Sign up to request clarification or add additional context in comments.

1 Comment

@Bekki Don't forget to choose as best answer. ;)

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.