javascript 如何将javascript数组转换为php数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6330830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:18:51  来源:igfitidea点击:

how to convert javascript array to php array

phpjavascript

提问by learnfromothers

here is the code to convert javascript array to php array..... this is done with cookies...

这是将javascript数组转换为php数组的代码.....这是用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>

is there any possible way to convert javascript array to php without cookies....

有没有什么可能的方法可以在没有 cookie 的情况下将 javascript 数组转换为 php....

回答by StanleyD

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'] ]

有时无法更改服务器端以将输出更改为 json。我输入了表格即 [ ['azz2465014416', '8', '22'],['azz2465014418', '10', '22'],['azz2465014420', '12', '22'],['azz2465014422', '14', '22'] ]

and I get php array from this input using this php code:

我使用这个 php 代码从这个输入中获取 php 数组:

<?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);

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.

然后您将在变量 $c 中获得结果输出。这不是理想的解决方案。它不会涵盖每个 javascript 数组,但如果您对某些数组有问题,您可以更改它以涵盖您的情况。

回答by JAAulde

In JS:

在JS中:

var a, s;
a = [ 'one', 'two', 'three'];
s = JSON.stringify( a );

//Do something to send `s` to the server in a request

In PHP, assuming scame from client in a cookie

在 PHP 中,假设s来自客户端的 cookie

<?php
$a = json_decode( $_COOKIE['s'], true );

Simple as that.

就那么简单。

回答by Amir

You Should Post it to another(or current) page to be processed with PHP. If you don't want page to be redirected Use AJAX to send Array to PHP.

您应该将其发布到另一个(或当前)页面以使用 PHP 进行处理。如果您不想重定向页面,请使用 AJAX 将 Array 发送到 PHP。

回答by masch

You can use JSON to do this. On the JavaScript-side, just use JSON.stringify(theArray);to create a JSON-representation of that array. In PHP you can use json_decode(theString, true);to get the array Links: http://php.net/manual/function.json-decode.phphttp://en.wikipedia.org/wiki/JSON

您可以使用 JSON 来执行此操作。在 JavaScript 端,只需用于JSON.stringify(theArray);创建该数组的 JSON 表示。在 PHP 中,您可以使用json_decode(theString, true);获取数组链接:http: //php.net/manual/function.json-decode.phphttp://en.wikipedia.org/wiki/JSON