Skip to content
PHP Logo

What are some common methods for passing arrays through links in PHP?

When passing arrays through links in PHP, one common method is to serialize the array using `serialize()` function before passing it as a query parameter in the URL. Another method is to convert the array into a JSON string using `json_encode()` and then decode it back to an array using `json_decode()` on the receiving end. Additionally, you can use `base64_encode()` and `base64_decode()` functions to encode and decode the array before passing it through the link.

php
// Method 1: Serialize the array
$array = [1, 2, 3];
$serializedArray = serialize($array);
$link = 'page.php?data=' . urlencode($serializedArray);

// Method 2: Convert array to JSON
$array = [1, 2, 3];
$jsonArray = json_encode($array);
$link = 'page.php?data=' . urlencode($jsonArray);

// Method 3: Base64 encode the array
$array = [1, 2, 3];
$encodedArray = base64_encode(serialize($array));
$link = 'page.php?data=' . urlencode($encodedArray);