1

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
  • 1
    Are you sure you want to encrypt an array? Quite unusual operation and unnecessary most of time Commented Apr 8, 2010 at 6:40

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

hmm... but it seem like only allow to encript for string but not array
@Jin Yong: No actually the array_map function walks through entire array thus encrypting/decrypting each value of the array hence the entire array :)
right, but do there have any way that we can use base64_encode come with password ot secret code?
@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
i tried the code that show on the link... Fatal error: Call to undefined function mcrypt_encrypt() show when i run the code
|
0

if you encrypt/decrypt string then use .

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

encrypt/decrypt $str_value

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

Comments

0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.