Serialised Array Walk

This is an array_walk style serialize that works with multi dimensional arrays, it also base64 encodes and decodes to allow ‘;’ to exist in the array.

 

$data[qwerty] = "blah";
$data[1][a] = "1aa";
$data[2][b] = "2bb";
$data[3][c] = ";;;;;;";

//To Serialise Call :

array_walk($data,'base64_encoder_multi');
$serialized_string = serialize($data);
//print $serialized_string . '\n'; //uncomment to see serialized array

//To Unserialize Call

$array = unserialize($serialized_string);
array_walk($array,'base64_cleaner_multi');

print_r($array);
//start functions
function base64_encoder_multi(&$val,$key) 
{
   if (is_array($val)) array_walk($val,'base64_encoder_multi',$new);
   else {
      $val = base64_encode($val);
   }
}

function base64_cleaner_multi(&$val,$key)
{
   if (is_array($val)) array_walk($val,'base64_cleaner_multi',$new);
   else {
      $val = base64_decode($val);
   }
}

?>
×

Comments are closed.