skip to Main Content

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 in, how can i do it?

Generally osCommerce asks for the single product insertion which in turn gives me back a product id which i pass into the url and get it in shopping cart where i am shown this added product, but now i have multiple products added in first page with different generated product ids and i have to display these products the same way they are displayed in genaral, for which i will need all these generated ids here in url

7

Answers


  1. You can either serialize() it or send it as ?a[]=1&a[]=val2&someOtherArg=val. This will give a $_GET array like:

    array(
        'a' => array(
            0 => '1',
            1 => 'val2',
        ),
        'someOtherArg' => 'val'
    )
    

    Do note, however, that you should probably keep your entire query below ~2k characters. (more)

    Login or Signup to reply.
  2. ?arr[]=abc&arr[]=pqr&arr[]=xyz&arr[]=xxx

    Login or Signup to reply.
  3. 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']);
    
    Login or Signup to reply.
  4. It should be sufficient to encode them like this:

    http://your.url/page.php?myArray%5B0%5D=val1&myArray%5B1%5D=val2

    Login or Signup to reply.
  5. 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, '', '&amp;');
    // foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor
    
    Login or Signup to reply.
  6. 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.

    Login or Signup to reply.
  7. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search