Thread: Passing Arrays

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    29
    Rep Power
    0
    I'm pretty new to php and can't work out how to pass an array from one module to another. I've tried
    <INPUT TYPE="hidden" NAME="array" VALUE="<?php echo "$array"; ?>"> </INPUT>
    but $array is not recognised in the called module. I reckon I could loop for all the elements in the array and echo a whole lot of hidden input fields, but it's not very elegant. Is there a better way?
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,646
    Rep Power
    4492
    I think you can use serialize()/unserialize() for things like this. they take the array, turn it into a string, then you pass the string to the next module through a hidden field or through the url, then unserialize it within the next module.

    you could also look at implode()/explode() which convert arrays into strings and strings into arrays...

    ---John Holmes...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    29
    Rep Power
    0
    Thanks, thought that serialize was the answer but even though $ary1 = array('red','green','blue');
    $arr = serialize($ary1);
    echo "Array is - $arr"; produces a serialized output
    Array is - a:3:{i:0;s:3:"red";i:1;s:5:"green";i:2;s:4:"blue";}
    when I do
    <INPUT TYPE="hidden" NAME="fred" VALUE="<?php echo "$arr"; ?>"> </INPUT>
    and try to
    $arr = unserialize($fred);
    echo "Hello. Array is - $arr<BR>";

    I just get
    Hello. Array is -
    It looks as though unserialize is not working for some reason. I'll now try implode/explode.

Similar Threads

  1. passing arrays to function problem
    By Jadams in forum C Programming
    Replies: 11
    Last Post: November 26th, 2003, 11:47 AM
  2. Replies: 9
    Last Post: November 23rd, 2003, 02:38 AM
  3. passing multidimensional arrays to functions
    By infamous41md in forum C Programming
    Replies: 2
    Last Post: October 10th, 2003, 02:33 PM
  4. Passing Arrays In URLs - A better way
    By alexp in forum PHP Development
    Replies: 5
    Last Post: September 10th, 2003, 08:48 AM
  5. Passing Arrays and Query
    By TuxLives in forum PHP Development
    Replies: 2
    Last Post: August 18th, 2003, 01:29 PM

IMN logo majestic logo threadwatch logo seochat tools logo