Thauwa Posted December 10, 2010 Share Posted December 10, 2010 Heya guys! What's up? OK here's my problem: I got a variable, $var1, which is to have values like "groan,100,200,350,50" I need to know how to split this $var1 into $split1, $split2, $split3, $split4, $split5, where $split1 = "groan"; $split2 = "100"; $split3 = "200"; $split4 = "350"; $split5 = "50"; Thanks in advance people! P.S. In my case I am going to use some of the values for calculations. So I don't think that its easy with arrays, which are difficult for me to understand . P.S.S. When I am retrieving data from the database, many variables like $var1 that need to be split are being extracted. I hope that you understand. Thanks and Regards, Thauwa Link to comment https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/ Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 use the explode function http://php.net/manual/en/function.explode.php It will return an array. $var1='groan,100,200,350,50' $array1 = explode(',',$var1); print_r($array1); foreach($array1 AS $var){ echo $var; } for($i=0;$i<count($array1);$i++){ ${'split'.$i}=$array1[$i]; } echo $split1.$split2.$var3.$split4.$split5; Link to comment https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/#findComment-1145354 Share on other sites More sharing options...
Thauwa Posted December 10, 2010 Author Share Posted December 10, 2010 Thanks for the quick reply! I think that that's what I wanted. One small annoying question. In: for($i=0;$i<count($array1);$i++){ ${'split'.$i}=$array1[$i]; } echo $split1.$split2.$var3.$split4.$split5; I necessarily needn't echo the data right? Thanks a lot MMDE. Link to comment https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/#findComment-1145357 Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 no, you don't. just trying to show you what it does quickly... Exactly what you want: $var1='groan,100,200,350,50'; $array1 = explode(',',$var1); for($i=0;$i<count($array1);$i++){ ${'split'.$i}=$array1[$i]; } I would have used the $array1 array though. like this: $array1['0'] that is the $split1 variable you want. $array1['1'] that is the $split2 variable you want. etc, just an explanation on how arrays work. Link to comment https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/#findComment-1145360 Share on other sites More sharing options...
BlueSkyIS Posted December 10, 2010 Share Posted December 10, 2010 FYI: There is no need to quote numeric array keys $array1[1] = ' is better'; Link to comment https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/#findComment-1145369 Share on other sites More sharing options...
Thauwa Posted December 10, 2010 Author Share Posted December 10, 2010 In all the manuals, tutorials and sample codes I've read, I've never found a better example showing the action of arrays. Thank you MMDE. I really owe you. And thanks BlueSkylS. I couldn't understand "quote numeric array keys", for I am new to arrays. If only you'd elaborate a bit........... Thanks, Regards and Well Wishes , Thauwa P.S. I was serious with my first sentence... Link to comment https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/#findComment-1145373 Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 for($i=0;$i<count($array1);$i++){ ${'split'.($i+1)}=$array1[$i]; } sorry, I forgot to make it $i+1 (the name of the split variables). only difference is that $split1==$array1[0] now. lol oh and what he meant is that 1 is a number, and not a string. meaning: array keys can be named lots of stuff, not just numbers, but also string, just like a variables. $arraykey='lol'; $array2[$arraykey]='hello world'; echo $array2[lol]; ^ won't work. echo $array2['lol']; ^ works. I need the apostrophes to show that it's a string, not a function or something else. if you try to echo only a number, you will see it works without apostrophes. It's the same thing here: $arraykey=1; $array3[$arraykey]='hello world'; echo $array3[1]; ^ works echo $array3['1']; ^ works Link to comment https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/#findComment-1145378 Share on other sites More sharing options...
Thauwa Posted December 10, 2010 Author Share Posted December 10, 2010 I have no words to express my gratitude. Thanks a lot MMDE! And everyone else who read this, thought about this and tried to help me. Link to comment https://forums.phpfreaks.com/topic/221220-splitting-one-value-into-many/#findComment-1145388 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.