horseatingweeds Posted July 31, 2007 Share Posted July 31, 2007 I'm having trouble understanding this of finding an example. How do I make a variable like this: $state = "\ US:AK:Alaska|\ US:AL:Alabama|\ US:AR:Arkansas|\ US:AS:American Samoa|\ US:AZ:Arizona|\ "; Into an array? I then want to cut each array value into its three parts so I can convert the state code to the actual name. Quote Link to comment https://forums.phpfreaks.com/topic/62590-how-do-i-turn-a-variable-into-an-array/ Share on other sites More sharing options...
AndyB Posted July 31, 2007 Share Posted July 31, 2007 explode() with |\ as delimiter, then explode() with : delimiter Quote Link to comment https://forums.phpfreaks.com/topic/62590-how-do-i-turn-a-variable-into-an-array/#findComment-311575 Share on other sites More sharing options...
horseatingweeds Posted July 31, 2007 Author Share Posted July 31, 2007 Right, I understand that. I'm confused how to get it into the arrays. Quote Link to comment https://forums.phpfreaks.com/topic/62590-how-do-i-turn-a-variable-into-an-array/#findComment-311588 Share on other sites More sharing options...
btherl Posted July 31, 2007 Share Posted July 31, 2007 If you want state code => name, try this: $state_arr = explode("|\n", $state); # if "|\n" is the delimineter $lookup_tab = array(); foreach ($state_arr as $s) { list($country, $code, $name) = explode(':', $s); $lookup_tab[$code] = $name; } print_r($lookup_tab); Quote Link to comment https://forums.phpfreaks.com/topic/62590-how-do-i-turn-a-variable-into-an-array/#findComment-311598 Share on other sites More sharing options...
horseatingweeds Posted July 31, 2007 Author Share Posted July 31, 2007 btherl, thanks. I'm understanding better now. Quote Link to comment https://forums.phpfreaks.com/topic/62590-how-do-i-turn-a-variable-into-an-array/#findComment-311635 Share on other sites More sharing options...
horseatingweeds Posted July 31, 2007 Author Share Posted July 31, 2007 How can I alter the above code to also include a numerical index? Like ($number, $country, $code, $name). Quote Link to comment https://forums.phpfreaks.com/topic/62590-how-do-i-turn-a-variable-into-an-array/#findComment-312078 Share on other sites More sharing options...
btherl Posted August 1, 2007 Share Posted August 1, 2007 Not 100% sure what you want here.. but maybe something like this: $state_arr = explode("|\n", $state); # if "|\n" is the delimineter $lookup_tab = array(); $i = 0; foreach ($state_arr as $s) { list($country, $code, $name) = explode(':', $s); print "$i, $country, $code, $name\n"; $lookup_tab[$code] = $name; $i++; } print_r($lookup_tab); I haven't actually stored the numerical index because I'm not sure where you want it. But that code should demonstrate how to make it available. Quote Link to comment https://forums.phpfreaks.com/topic/62590-how-do-i-turn-a-variable-into-an-array/#findComment-312432 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.