I am attempting to pass an array as a page argument in a page callback in one of my modules. The scenario is this: The user selects some photo prints and I'm trying to add all the selected photo prints to the cart. So I'm trying to pass an array through a url to use it in a function. And the photo prints can be from different photo albums. There is code to do it if all the photos are from the same album so Im currently perusing that as well. This is code I "inherited" by the way.

The URL looks like /add_selected_prints_to_cart/Array. As you can see, instead of an array of values, it's just Array.

This is the page callback:

// Add the selected prints to the cart.
$items['add_selected_prints_to_cart/%'] = array(
  'page callback' => 'add_selected_prints_cart',
  'page arguments' => array(1),
  'access callback' => TRUE,
  'access arguments' => array('view own gallery'),
  'type' => MENU_CALLBACK

Like I said, I am just trying to figure out how to pass the array. Once I figure out that, the other part is easy.

Maybe there is an easier way to do this.


1 Answer 1


That happens because the code you are using is similar to the following one.

$url = url('add_selected_prints_to_cart/' . $array);

If each photo has an ID that can be used to univocally identify each photo, in the same way a node ID univocally identify a node, you could use code similar to the following to create the URL. I assume the structure of the $photos array is the following one.

  'photo ID 1' => array( /* data for the photo with ID equal to 1 */),
  'photo ID 2' => array( /* data for the photo with ID equal to 2 */),
  'photo ID 3' => array( /* data for the photo with ID equal to 3 */),  
  // ...
$url = url('add_selected_prints_to_cart/' . implode(',', array_keys($photos)));

In this case, add_selected_prints_cart() can get the photo IDs using code similar to the following one.

function add_selected_prints_cart($photos) {
  $photo_ids = explode(',', $photos);

  // Load the photos from the database.

If you need to pass the array to the menu callback, I would add the array to the session variables. In this way the code would for defining the menu callback would be similar to the following one.

function mymodule_menu() {
  $items['mymodule_add_selected_prints_to_cart'] = array(
    'page callback' => 'add_selected_prints_cart',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK

  return $items;

function mymodule_add_selected_prints_to_cart() {
  $photos = $_SESSION['mymodule_selected_photos'];
  // ...

(If you use 'access callback' => TRUE, the access arguments are not used.)

Otherwise, the code to convert an array to a string is similar to the following one.

$string = strtr(base64_encode(serialize($array)), array('+' => '-', '/' => '_', '=' => ''));

The code to obtain the array back from $string is the following one.

$array = unserialize(base64_decode(strtr($string, array('-' => '+', '_' => '/'))));

Bear in mind there is a limit for the URL length accepted from browsers.

  • Yeah, the array structure is just like that. Thanks, this is really helpful.
    – user
    Commented Dec 13, 2012 at 13:51
  • Maybe I should just use POST data for this instead
    – user
    Commented Dec 13, 2012 at 14:54
  • Using POST data from a form for another form is a more complicated. I would suggest using session data, especially if the user is expected to submit to the second form right after the first.
    – apaderno
    Commented Dec 13, 2012 at 15:08
  • I hadn't considered using session data. M y thought was to create a hidden form variable and send it that way, but I tried adding it to the session data and that works too.
    – user
    Commented Dec 13, 2012 at 15:34
  • By sending it that way, I wouldn't have to worry about the length of the url either.
    – user
    Commented Dec 13, 2012 at 15:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.