I have an ajax posted parameter as below:
$.ajax({ url:'url', data:{ary:my_ary}, ...
Where value in the my_ary = Array([0]=> Test Text,[1]=> test Text2)
Now i need to get values from this array using a foreach() loop like this:
foreach($_POST['ary'] as $val){ echo($val.'<br>'); }
But it is showning the following error
An invalid arguments passed to foreach loop
Advertisement
Answer
Convert the array to a string before passing it like so:
my_ary.join(',');
Or if it’s a complex array consider JSON:
JSON.stringify(my_ary);
In case of your associative array
$.ajax({ url:'url', data:{ my_ary:JSON.stringify(my_ary); }