2

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>

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

3
  • 3
    json encode then store into the cookie, decode back in php Commented Jun 13, 2011 at 13:12
  • @Marcel Korpel @Luzhin @venimus i need to convert javascript array to php array without using cookies... how can we do that.... any efficient way??......... Commented Jun 13, 2011 at 13:15
  • 3
    i assume you will use it in a PHP script, then you could use a_php=encodeURIComponent(JSON.stringify(a)) then use a_php as a GET or POST parameter to the php script that uses it. there you decode it with json_decode(rawurldecode($_GET['a_php'])) Commented Jun 13, 2011 at 13:29

4 Answers 4

8

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

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

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.

6

In 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 s came from client in a cookie

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

Simple as that.

2
  • i need to convert javascript array to php array without using cookies... how can we do that.... any efficient way??......... Commented Jun 13, 2011 at 13:16
  • This is the correct solution. Just use json_decode($JSArrayString, true)... simple! Commented Oct 22, 2018 at 4:01
4

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.

4
  • @Amir i need to convert javascript array to php array without using cookies... how can we do that.... any efficient way??......... Commented Jun 13, 2011 at 13:16
  • I didn't talk about cookies. Directly send array using POST and then parse with php. Commented Jun 13, 2011 at 13:28
  • I told u so. I always use jQuery and I pass array using a single string(keys and values in format of csv like) and then split using PHP Commented Jun 13, 2011 at 13:53
  • or maybe just JSON.stringify() ;) Dinosaurs :D Commented Oct 1, 2014 at 18:19
2

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.php http://en.wikipedia.org/wiki/JSON

3
  • i need to convert javascript array to php array without using cookies... how can we do that.... any efficient way??......... Commented Jun 13, 2011 at 13:16
  • 5
    @learn: Oh please, did you actually read these answers and try something yourself? You're merely copying the same comment over and over again. Commented Jun 13, 2011 at 13:17
  • @learn: See json.org. I strongly advice you to read a good JavaScript book before going on. Commented Jun 13, 2011 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.