Jump to content

Recommended Posts

I've had my own and found a few at php.net but is there something that can take an array and convert it to a string so that I can then take that string and set a variable equal to it?

 

What I have used in the past for 1 dimensional arrays used loops and took a horrendous amount of time to execute if the array was big.  I'd need something to be able to convert a 2d array to a string.

 

So for example

 

$myarray=Array('keyone' => Array('keyone' => 'valueone', 'keytwo' => 'valuetwo'), 'keytwo' => Array('keyone' => 'valueone', 'keytwo' => 'valuetwo'));

 

Is a common array I format I use.  I tried using foreach but I've been stuck for about 2 hours and I've got nothing.  So bottom line is that I need to get that array to turn into the string:

 

"Array('keyone' => Array('keyone' => 'valueone', 'keytwo' => 'valuetwo'), 'keytwo' => Array('keyone' => 'valueone', 'keytwo' => 'valuetwo'));"

 

I'm not sure what I should be using to do it.  The main factor is speed.  If the array has 10 keys and each of those has 5 keys I'd like to be able to get it to run in less than 30 seconds.  I'm thinking maybe foreach or having a function call on itself but I have no clue if that is too much work or just wrong.

 

Thanks in advance for any help/advice

Link to comment
https://forums.phpfreaks.com/topic/85299-solved-array-to-string/
Share on other sites

What are you planning to do with the resulting string?

 

If you're going to store it somewhere, I would use the serialize() function.

 

<?php
$myarray=Array('keyone' => Array('keyone' => 'valueone', 'keytwo' => 'valuetwo'), 'keytwo' => Array('keyone' => 'valueone', 'keytwo' => 'valuetwo'));
$sa = serialize($myarray);
echo $sa . "<br>";
?>

This results in a string like

a:2:{s:6:"keyone";a:2:{s:6:"keyone";s:8:"valueone";s:6:"keytwo";s:8:"valuetwo";}s:6:"keytwo";a:2:{s:6:"keyone";s:8:"valueone";s:6:"keytwo";s:8:"valuetwo";}}

To recreate the original array you would use the unserialize() function:

<?php
$new_array = unserialize($sa);
echo '<pre>' . print_r($new_array,true) . '</pre>';
?>

 

If you really want the original string you mentioned, you would use the var_export() function:

<?php
$myarray=Array('keyone' => Array('keyone' => 'valueone', 'keytwo' => 'valuetwo'), 'keytwo' => Array('keyone' => 'valueone', 'keytwo' => 'valuetwo'));
$x = var_export($myarray,true);
$str =  str_replace(',)',')',str_replace(array("\n",' '),'',$x));
echo $str . '<br>';
?>

 

Ken

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.