Sesquipedalian Posted January 1, 2008 Share Posted January 1, 2008 Hey everyone, What I'm curious of, is how do you put all parts of an array side by side into one variable? What I mean is if you have array('a', 'b', 'c', 'd') how do you take it and put it into a variable, so the new variable ends up being $var = 'abcd'; , where all of the array parts are next to eachother. Appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/83948-putting-all-parts-of-an-array-into-one-variable/ Share on other sites More sharing options...
Daniel0 Posted January 1, 2008 Share Posted January 1, 2008 $array = array('a', 'b', 'c', 'd'); $var = join('', $array); // $var contains: abcd Quote Link to comment https://forums.phpfreaks.com/topic/83948-putting-all-parts-of-an-array-into-one-variable/#findComment-427205 Share on other sites More sharing options...
Northern Flame Posted January 1, 2008 Share Posted January 1, 2008 try something like this: <?php $array = array('a', 'b', 'c', 'd'); foreach($array as $letter){ $var .= $letter; } echo $var; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83948-putting-all-parts-of-an-array-into-one-variable/#findComment-427207 Share on other sites More sharing options...
Northern Flame Posted January 1, 2008 Share Posted January 1, 2008 oh i didnt see daniel0's post, use his method instead of mine, mine is untested Quote Link to comment https://forums.phpfreaks.com/topic/83948-putting-all-parts-of-an-array-into-one-variable/#findComment-427208 Share on other sites More sharing options...
Sesquipedalian Posted January 1, 2008 Author Share Posted January 1, 2008 thanks.. that answered my question.. but i realize it didn't help what i was trying to do. I have a string full of parts that are seperated by |, so something like this: $string = '| This | is | a | part | This | is | a | diff | part'; The string updates when i add to it (not manually), and so usually what i do to get the different parts is I use explode. Now what I want to do is make it so that after every 4 array parts (like $string[1], $string[2], $string[3], $string[$4]) it to have a '<br />'; so that instead, $string would become 'This is a part <br />This is a diff <br />part' How can I do that? I want it to do it until it gets to the end of the array, so that I can have as much text as I want, and it will just make it like that. I've tried different things, and I'm getting frustrated.. So.. how? Quote Link to comment https://forums.phpfreaks.com/topic/83948-putting-all-parts-of-an-array-into-one-variable/#findComment-427217 Share on other sites More sharing options...
Daniel0 Posted January 1, 2008 Share Posted January 1, 2008 <?php $parts = array('This','is','a','part','This','is','a','diff','part'); $slices = array(); $lines = ceil(count($parts)/4); for($i = 0; $i < $lines; $i++) { $slices[] = array_slice($parts, $i*4, 4); } ?> Will split it into slices of up to four. Just join them like above again. Quote Link to comment https://forums.phpfreaks.com/topic/83948-putting-all-parts-of-an-array-into-one-variable/#findComment-427244 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.