how to convert javascript array to php array

前端 未结 4 1548
轮回少年
轮回少年 2021-01-07 09:04

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         


        
相关标签:
4条回答
  • 2021-01-07 09:26

    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.

    0 讨论(0)
  • 2021-01-07 09:32

    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.

    0 讨论(0)
  • 2021-01-07 09:42

    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.

    0 讨论(0)
  • 2021-01-07 09:45

    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

    0 讨论(0)
提交回复
热议问题