I have an ajax posted parameter as below:
我有一个ajax发布的参数如下:
$.ajax({
url:'url',
data:{ary:my_ary},
...
Where value in the my_ary = Array([0]=> Test Text,[1]=> test Text2)
其中my_ary =数组中的值([0]=>测试文本,[1]=>测试文本2)
Now i need to get values from this array using a foreach() loop like this:
现在我需要使用foreach()循环从这个数组中获取值,如下所示:
foreach($_POST['ary'] as $val){
echo($val.'<br>');
}
But it is showning the following error
但它显示了以下错误
An invalid arguments passed to foreach loop
传递给foreach循环的无效参数
2 个解决方案
#1
2
Convert the array to a string before passing it like so:
将数组转换为字符串,然后像这样传递:
my_ary.join(',');
Or if it's a complex array consider JSON:
如果是复杂数组,考虑JSON:
JSON.stringify(my_ary);
In case of your associative array
如果是关联数组
$.ajax({
url:'url',
data:{
my_ary:JSON.stringify(my_ary);
}
#2
1
$.ajax({
url:'url',
data:JSON.stringify(my_ary)
you need parse arrays into the string.
需要将数组解析到字符串中。
I hope it helps you
我希望它能帮助你
#1
2
Convert the array to a string before passing it like so:
将数组转换为字符串,然后像这样传递:
my_ary.join(',');
Or if it's a complex array consider JSON:
如果是复杂数组,考虑JSON:
JSON.stringify(my_ary);
In case of your associative array
如果是关联数组
$.ajax({
url:'url',
data:{
my_ary:JSON.stringify(my_ary);
}
#2
1
$.ajax({
url:'url',
data:JSON.stringify(my_ary)
you need parse arrays into the string.
需要将数组解析到字符串中。
I hope it helps you
我希望它能帮助你