writer Posted April 21, 2008 Share Posted April 21, 2008 I'd like to create a simple input form (field + submit button) that takes a comma separated list of locations (Paris, Hungary, Prague, etc.), strips the comma and turns the answers into an array ( $ar_locations = (1->Paris, 2->Hungary... etc.) ). Any idea of some resources with which I can do this? Link to comment https://forums.phpfreaks.com/topic/102068-turning-a-comma-seperated-list-into-an-array/ Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 $expld_array = explode(';' $string_w_semicolon); Fill it in with the right string, lol. Link to comment https://forums.phpfreaks.com/topic/102068-turning-a-comma-seperated-list-into-an-array/#findComment-522430 Share on other sites More sharing options...
kenrbnsn Posted April 21, 2008 Share Posted April 21, 2008 That syntax is incorrect. Do this instead: <?php $str = 'This,is,a,comma,separated,list,of,values'; $ary = explode(',',$str); echo '<pre>' . print_r($ary,true) . '</pre>'; // show the new array ?> Ken Link to comment https://forums.phpfreaks.com/topic/102068-turning-a-comma-seperated-list-into-an-array/#findComment-522445 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 That syntax is incorrect. Do this instead: <?php $str = 'This,is,a,comma,separated,list,of,values'; $ary = explode(',',$str); echo '<pre>' . print_r($ary,true) . '</pre>'; // show the new array ?> Ken Ahahaha, I thought he said semicolon. I need to read more careful. Thanks. Link to comment https://forums.phpfreaks.com/topic/102068-turning-a-comma-seperated-list-into-an-array/#findComment-522447 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 $expld_array = explode(';' $string_w_semicolon); Fill it in with the right string, lol. This is correct. $csv = "Comma,separated,thing,"; $expld_array = explode(',' $csv); Fill it in with the right string. Thanks, to the dude who corrected me. Link to comment https://forums.phpfreaks.com/topic/102068-turning-a-comma-seperated-list-into-an-array/#findComment-522449 Share on other sites More sharing options...
writer Posted April 21, 2008 Author Share Posted April 21, 2008 Yeah, I did a surface-level Google dive and found nothing. As soon as I found explode() I felt like an idiot. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/102068-turning-a-comma-seperated-list-into-an-array/#findComment-522591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.