#native_company# #native_desc#
#native_cta#

Associative Array to String and String to Associative Array functions

By Stephen Calia
on January 31, 2005

I like writng in PHP, but miss the easy perl way of changing an associative array to a list and then a list to a string. This makes it handy to pass a hidden value of an array as a string and then convert it back to an array when the next step of the script needs it. So, I wrote a couple of functions to do just that.

//array to string
function arraytostr ($array=array()) {

     $length = 0;
     $foreach ($array as $key => $value) {
          $keystring .= "$key ";
          $valuestring .= "$value ";
          $length++;
     }
		
     return array($length,$keystring,$valuestring);
}
//sting to array	
function strtoarray ($length="",$keystring="",$valuestring="") {
          $keys = explode(" ",$keystring);
          $values = explode(" ",$valuestring);
          for ($i=0; $i < $length; $i++) {
		$key = $keys[$i];
		$newarray[$key] = $values[$i];
          }
		
          return $newarray;
}