kratsg Posted October 22, 2007 Share Posted October 22, 2007 We should all be familiar with the explode() and implode() functions. explode("Some String") --> "Some Array" implode("Some Array") --> "Some String" The idea is that each function goes back and forth between an array and string, provided you use a common delimiter. Now, let's look at a simple example, before I post my issue. $stuff_array = array("Hello","Cool","Some","Regular Array","Used"); print_r($stuffy_array); $stuff_string = implode("|",$stuff_array); echo $stuff_string; $stuffs_array = explode("|",$stuff_string); print_r($stuffs_array); //output of $stuff_array [0] => Hello, [1] => Cool, [2] => Some, [3] => Regular Array, [4] => Used //output of $stuff_string Hello|Cool|Some|Regular Array|Used [0] => Hello, [1] => Cool, [2] => Some, [3] => Regular Array, [4] => Used Now, let's use associative arrays instead: $stuff_array = array("First"=>"Hello","Second"=>"Cool","Third"=>"Some","Fourth"=>"Regular Array","Fifth"=>"Used"); print_r($stuffy_array); $stuff_string = implode("|",$stuff_array); echo $stuff_string; $stuffs_array = explode("|",$stuff_string); print_r($stuffs_array); //output of $stuff_array [First] => Hello, [second] => Cool, [Third] => Some, [Fourth] => Regular Array, [Fifth] => Used //output of $stuff_string Hello|Cool|Some|Regular Array|Used [0] => Hello, [1] => Cool, [2] => Some, [3] => Regular Array, [4] => Used So, take a look at this. When we use non-associative arrays, our data is preserved. However, when we deal with associative arrays, we lose the index values, and they get replaced with the default numbering system. Has anyone ideas on how to preserve the indexes as we convert to a string, and reconvert back? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 22, 2007 Share Posted October 22, 2007 If you want to make an array into a string and take that string and turn back into the original array, you don't use implode/explode, but the serialize() and unserialize() functions: <?php $stuff_array = array("First"=>"Hello","Second"=>"Cool","Third"=>"Some","Fourth"=>"Regular Array","Fifth"=>"Used"); echo '<pre>' . print_r($stuffy_array,true) . '</pre>; $stuff_string = serialize($stuff_array); echo $stuff_string . '<br>'; $stuffs_array = unserialize($stuff_string); echo '<pre>' . print_r($stuffs_array,true) . '</pre>; ?> This will work with any array. Ken Quote Link to comment Share on other sites More sharing options...
dbo Posted October 22, 2007 Share Posted October 22, 2007 What's the problem? Write your own implode function if you want to use it to do something other than what it was intended for.... function kratsg_implode($keyseperator, $valueseperator, $list) { $rv = ""; foreach( $list As $key => $value ) { $rv .= ($key . $keyseperator . $value . $valueseperator); } return $rv; } $list = Array(); $list['one'] = "some stuff"; $list['two'] = "some more stuff"; $list['thr'] = "other stuff"; $value = kratsg_implode(":", "|", $list); echo $value; Or something similar to the above. Quote Link to comment Share on other sites More sharing options...
dbo Posted October 22, 2007 Share Posted October 22, 2007 I would tend to agree with kenrbnsn and go with the serialize/unserilize approach. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.