通过ajax将数组传递给php

时间:2022-06-10 02:25:20

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

我通过ajax将数组传递给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.

如何将每个值分配给php中的变量?我试过这个,但没有做我需要的。

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

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

预期$ var1 =“one”,$ var2 =“two”等...

1 个解决方案

#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并使用。创建动态变量。

 ${"var" . ($i+1)} = $_POST['arr'][$i];

#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并使用。创建动态变量。

 ${"var" . ($i+1)} = $_POST['arr'][$i];