Jump to content

Interesting insight into associative arrays versus non-associatives...


kratsg

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.