Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Assoc Array to String & String to Assoc Array (http://www.go4expert.com/articles/assoc-array-string-string-assoc-array-t469/)

pradeep 17Oct2005 22:00

Assoc Array to String & String to Assoc Array
 
I like coding PHP, in Perl we can change an associative array to a list then to a string. In case if we wish to do the same in PHP, we need to write some user functions for that purpose.
Here are two functions which will convert Array to String and vice-versa.

PHP Code:

//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;
 } 



All times are GMT +5.5. The time now is 05:01.