ultraviolet_998 Posted March 17, 2007 Share Posted March 17, 2007 Hi Everyone, I've been really battling with this feature. I seem to have it right up until the part of rebuilding the array from the string. Herewith is my array2string code: function api_array2string($array) { $return = ""; $null_value = ""; foreach ($array as $key => $value) { if (is_array($value)) { $return_value = "(".api_array2string($value); } else { $return_value = $value."|"; } $return .= $key."=".$return_value; } $return .= ")"; return $return; } Which converts the following array into the following string: Array ( [wd] => Array ( [0] => Array ( [0] => wd.co.za [realm] => wd.co.za [1] => SHAPED [service] => SHAPED [2] => Shaped ADSL [description] => Shaped ADSL [3] => wd [userName] => wd ) [1] => Array ( [0] => wdunshaped.co.za [realm] => wdunshaped.co.za [1] => UNSHAPED [service] => UNSHAPED [2] => Unshaped ADSL [description] => Unshaped ADSL [3] => wd [userName] => wd ) ) [test] => Array ( [0] => Array ( [0] => test.com [realm] => test.com [1] => SHAPED [service] => SHAPED [2] => Shaped ADSL [description] => Shaped ADSL [3] => test [userName] => test ) [1] => Array ( [0] => test-local.com [realm] => test-local.com [1] => LOCAL [service] => LOCAL [2] => Local-only ADSL [description] => Local-only ADSL [3] => test [userName] => test ) ) ) array(wd=array(0=array(0=wd.co.za,realm=wd.co.za,1=SHAPED,service=SHAPED,2=Shaped ADSL,description=Shaped ADSL,3=wd,UserName=wd);1=array(0=wdunshaped.co.za,realm=wdunshaped.co.za,1=UNSHAPED,service=UNSHAPED,2=Unshaped ADSL,description=Unshaped ADSL,3=wd,UserName=wd));test=array(0=array(0=test.com,realm=test.com,1=SHAPED,service=SHAPED,2=Shaped ADSL,description=Shaped ADSL,3=test,UserName=test);1=array(0=test-local.com,realm=test-local.com,1=LOCAL,service=LOCAL,2=Local-only ADSL,description=Local-only ADSL,3=test,UserName=test))) Now I was using strpos to run through the string isolating the "(" and then cutting up the string as it went on, but it seems I can't get it to work. Any suggestions on how to get this to work, or perhaps my string is retarded and can be simplified? Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/43172-string-to-array/ Share on other sites More sharing options...
kenrbnsn Posted March 17, 2007 Share Posted March 17, 2007 What are you trying to do? Perhaps you should look at the serialize() and unserialize() functions. Ken Link to comment https://forums.phpfreaks.com/topic/43172-string-to-array/#findComment-209626 Share on other sites More sharing options...
ultraviolet_998 Posted March 17, 2007 Author Share Posted March 17, 2007 Hi Ken, Am trying convert an array to string so that I can then POST it to another page. This page will then rebuild the array and format the data in that array accordingly. Would serialize do such a thing? Thanks Link to comment https://forums.phpfreaks.com/topic/43172-string-to-array/#findComment-209629 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2007 Share Posted March 17, 2007 Yes, that what the functions serialize() and unserialize() do. The serialize() function will convert an array to a string and the unserialize() function will recreate the array from the serialized string. Read the manual references I posted. Ken Link to comment https://forums.phpfreaks.com/topic/43172-string-to-array/#findComment-209630 Share on other sites More sharing options...
ultraviolet_998 Posted March 17, 2007 Author Share Posted March 17, 2007 Hmmm, I feel like a fool! Thank you so much for pointing this out to me. I always assumed (yes I did not read) that serialize and unserialize was to do with sessions. Thanks once again! Link to comment https://forums.phpfreaks.com/topic/43172-string-to-array/#findComment-209633 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.