通过ajax将数组传递给php
问题描述:
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...
我通过ajax将数组传递给php。 p>
$交( “send.php”,{ARR:ARR})。 // [“one”,“two”,“three”] code> pre>如何在php中为变量分配每个值? 我尝试了这个,但没有做我需要的。 p>
for($ i = 0; $ i&lt; $ _POST ['arr']; $ i ++){ $ var +($ i + 1)= $ _POST ['arr'] [$ i]; } code> pre>
预期$ var1 =“one”,$ var2 =“two”等... p> div>
答
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];