mme Posted May 16, 2011 Share Posted May 16, 2011 Hi, I am trying to split a string into an array and then seeing what sort of data it is. eg; I get the following string api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse I need to be able to put this into an array so in this case myarray[type]=somethingelse myarray[api]=somerandomkeyhere myarray[user]=someuser myarray[password]=somehashvaluehere but it could include many more/less different value's separated by the & symbol. Thanks, -mme Quote Link to comment https://forums.phpfreaks.com/topic/236548-splitting-data-into-arrays/ Share on other sites More sharing options...
HDFilmMaker2112 Posted May 16, 2011 Share Posted May 16, 2011 $strings=explode("&","string here") Gets you: $strings array("api=somerandomkey", "user=someuser", "ect.") foreach($strings as $string){ $string=explode("=",$string); } $string array("api","somerandomkey", "user", "someuser", "ect.") $string[0]=$string[1]; $string[0] could also be $string["api"] Quote Link to comment https://forums.phpfreaks.com/topic/236548-splitting-data-into-arrays/#findComment-1216033 Share on other sites More sharing options...
kenrbnsn Posted May 16, 2011 Share Posted May 16, 2011 Use the function parse_str <?php $str = 'api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse'; parse_str($str,$ary); print_r($ary); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/236548-splitting-data-into-arrays/#findComment-1216038 Share on other sites More sharing options...
mme Posted May 16, 2011 Author Share Posted May 16, 2011 Use the function parse_str <?php $str = 'api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse'; parse_str($str,$ary); print_r($ary); ?> Ken Thanks I did not know such a simple function existed. This was exactly what I was looking for. -mme Quote Link to comment https://forums.phpfreaks.com/topic/236548-splitting-data-into-arrays/#findComment-1216040 Share on other sites More sharing options...
HDFilmMaker2112 Posted May 16, 2011 Share Posted May 16, 2011 Use the function parse_str <?php $str = 'api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse'; parse_str($str,$ary); print_r($ary); ?> Ken Thanks I did not know such a simple function existed. This was exactly what I was looking for. -mme Neither did I. Much simpler than my method. Quote Link to comment https://forums.phpfreaks.com/topic/236548-splitting-data-into-arrays/#findComment-1216042 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.