How can an array be uniquely encrypted and decrypted in PHP?

To uniquely encrypt and decrypt an array in PHP, you can serialize the array into a string before encryption and then unserialize it after decryption. This ensures that the array structure is preserved during encryption and decryption. You can use a secure encryption algorithm like AES for encryption and decryption.

<?php
// Function to encrypt an array
function encryptArray($array, $key) {
    $data = serialize($array);
    $encrypted = openssl_encrypt($data, 'AES-256-CBC', $key, 0, '1234567890123456');
    return base64_encode($encrypted);
}

// Function to decrypt an array
function decryptArray($encrypted, $key) {
    $data = base64_decode($encrypted);
    $decrypted = openssl_decrypt($data, 'AES-256-CBC', $key, 0, '1234567890123456');
    return unserialize($decrypted);
}

// Example array
$array = array('name' => 'John', 'age' => 30);

// Encryption key
$key = 'secretkey';

// Encrypt the array
$encrypted = encryptArray($array, $key);
echo "Encrypted: " . $encrypted . "\n";

// Decrypt the array
$decryptedArray = decryptArray($encrypted, $key);
print_r($decryptedArray);
?>