53329 Posted January 10, 2008 Share Posted January 10, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/85299-solved-array-to-string/ Share on other sites More sharing options...
Ken2k7 Posted January 10, 2008 Share Posted January 10, 2008 $myString = implode(",",$myarray); Quote Link to comment https://forums.phpfreaks.com/topic/85299-solved-array-to-string/#findComment-435229 Share on other sites More sharing options...
btherl Posted January 10, 2008 Share Posted January 10, 2008 30 seconds? You should thinking in milliseconds. A function calling itself (recursion) is probably the easiest way to go about it. Is it a requirement that the output be valid php code? Quote Link to comment https://forums.phpfreaks.com/topic/85299-solved-array-to-string/#findComment-435232 Share on other sites More sharing options...
kenrbnsn Posted January 10, 2008 Share Posted January 10, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/85299-solved-array-to-string/#findComment-435237 Share on other sites More sharing options...
53329 Posted January 10, 2008 Author Share Posted January 10, 2008 And thats why you're the GM lol. Thats a bunch man. I didn't even think of that. Thats a huge time saver. Quote Link to comment https://forums.phpfreaks.com/topic/85299-solved-array-to-string/#findComment-435310 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.