tags:

views:

97

answers:

3

Does anyone know do there have any way that I can encrypt the array in php??

for example:

$arr_value = array("1","2","3","4");

any way that I can encrypt $arr_value, also decrypt it later ini php?

+1  A: 

First, see this please.

You can encrypt/decrypt something like this:

$arr_value = array("1","2","3","4");

function encrypt($text)
{
   return base64_encode($text);
}

function decrypt($text)
{
   return base64_decode($text);
}

Now to encrypt:

$encrypted = array_map("encrypt", $arr_value);
echo '<pre>';
print_r($encrypted);

And to decrypt:

$decrypted = array_map("decrypt", $arr_value);
echo '<pre>';
print_r($decrypted);

.

Note:

It is worth having a look at a better way of encryption library:

The mcrypt library.

Sarfraz
hmm... but it seem like only allow to encript for string but not array
Jin Yong
@Jin Yong: No actually the `array_map` function walks through entire array thus encrypting/decrypting each value of the array hence the entire array :)
Sarfraz
right, but do there have any way that we can use base64_encode come with password ot secret code?
Jin Yong
@Jin Yong: Yes you can encrypt secret codes of course. I would suggest you to encrypt like told in the link i posted on top of my answer. Thanks
Sarfraz
i tried the code that show on the link... Fatal error: Call to undefined function mcrypt_encrypt() show when i run the code
Jin Yong
Hey @Jin do you understand that this code encrypt nothing?
Col. Shrapnel
why down vote please...............?
Sarfraz
@Jin Yong: because you are getting the error unknonw mycrupt library, it seems that it has not been enabled there on your server. You can use my code which doesn't have any dependencies.
Sarfraz
A: 

You might enjoy a look at mcrypt. I'm not sure where you're storing the encrypted values, or what cipher you want to use. mcrypt should let you accomplish whatever you need.

Examples are here.

Tim Post
A: 

if you encrypt/decrypt string then use .

$str_value = $arr_value.join(",");

encrypt/decrypt $str_value

$arr_value=$str_value.split(",")
Salil