问题描述
这里是将javascript数组转换为php数组的代码.....这是用cookies完成的......
here is the code to convert javascript array to php array..... this is done with cookies...
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie()
{
var a_php = "";
var a=new Array();
/*
a[0]="Saab";
a[1]="Volvo";
a[2]="BMW";
a[3]="sample"
a[4]="Saab";
a[5]="Volvo";
a[6]="BMW";
a[7]="sample"
a[8]="Saab";
a[9]="Volvo";
a[10]="BMW";
a[11]="sample"
a[12]="Saab";
a[13]="Volvo";
a[14]="BMW";
a[15]="sample"
a[16]="Saab";
a[17]="Volvo";
a[18]="BMW";
a[19]="sample"
a[20]="Saab";
a[21]="Volvo";
a[22]="BMW";
a[23]="sample"
a[24]="Saab";
a[25]="Volvo";
a[26]="BMW";
a[27]="sample"
*/
for(var t=0;t<350;t++)
a[t]="hello"+t;
for(var count=0;count<a.length;count++)
a_php = a_php+a[count]+",";
setCookie("new_cookie1",a_php,3);
}
</script>
是否有任何可能的方法将javascript数组转换为不带cookie的php ....
is there any possible way to convert javascript array to php without cookies....
推荐答案
有时无法更改服务器端以将输出更改为json。我输入的形式是
[['azz2465014416','8','22'],['azz2465014418','10','22'],['azz2465014420' ,''12','22'],['azz2465014422','14','22']]
Sometimes it is not possible to change server side to change output to json. I had input in the form i.e.[ ['azz2465014416', '8', '22'],['azz2465014418', '10', '22'],['azz2465014420', '12', '22'],['azz2465014422', '14', '22'] ]
我得到的PHP使用此PHP代码从此输入数组:
and I get php array from this input using this php code:
<?php
$a = "[ ['azz2465014416', '8', '22'],['azz2465014418', '10', '22'],['azz2465014420', '12', '22'],['azz2465014422', '14', '22'] ]";
$b = '$c = '.(str_replace(array('[',']'),array('array(',')'),$a)).';';
eval($b);
print_r($c);
您将获得可变$ c结果输出。这不是理想的解决方案。它不会覆盖每个javascript数组,但是如果你遇到某些数组有问题,你可以更改它来覆盖你的情况。
you will have then in varible $c resulted output. This is not ideal solution. It will not cover every javascript array, but if you have trouble with some array, you can change it to cover your situation.
这篇关于如何将javascript数组转换为php数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!