how to send array values through url in PHP?

前端 未结 7 1293
情话喂你
情话喂你 2020-12-16 07:56

how to send array through url in PHP?

I have an array of product ids i want to use these id through url because this is the osCommerce needs it in which i am working

相关标签:
7条回答
  • 2020-12-16 08:17

    well what i would do is json_encode(php json) the array and assign that to a variable in php. you can then urlencode the variable to send it via the url. On the other end you can json_decode. Do look up for json if you are not aware of it. its very powerful and useful though.

    0 讨论(0)
  • 2020-12-16 08:23

    ?arr[]=abc&arr[]=pqr&arr[]=xyz&arr[]=xxx

    0 讨论(0)
  • 2020-12-16 08:25

    Your looking for http_build_query().

    Example from php.net:

    $data = array('foo'=>'bar',
                  'baz'=>'boom',
                  'cow'=>'milk',
                  'php'=>'hypertext processor');
    
    echo http_build_query($data);
    // foo=bar&baz=boom&cow=milk&php=hypertext+processor
    
    echo http_build_query($data, '', '&');
    // foo=bar&baz=boom&cow=milk&php=hypertext+processor
    
    0 讨论(0)
  • 2020-12-16 08:27

    It should be sufficient to encode them like this:

    http://your.url/page.php?myArray[0]=val1&myArray[1]=val2

    0 讨论(0)
  • 2020-12-16 08:38

    If you are POST-ing data, then name your fields with PHP array-style syntax:

    <input type="text" name="myArray[]" value="A">
    <input type="text" name="myArray[]" value="B">
    <input type="text" name="myArray[]" value="C">
    

    If you want to pass the data in a GET request, you can separate the data and split it server side using explode:

    page.php?myData=A,B,C,D
    

    ...

    $myArray = explode(',', $_POST['myData']);
    
    0 讨论(0)
  • 2020-12-16 08:40

    If you already have the product IDs in an array, then you can use the http_build_query() function, which will encode the array like thus:

    http://www.example.com/?pid[]=1&pid[]=2&pid[]=3 ...

    Hope that helps.

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