dogfighter Posted September 23, 2008 Share Posted September 23, 2008 I'm able to take a string, split it up with preg_split, and then create an array using the output. However, now I want to echo them all together in random order, and each only once. So if my array looks like this... $color[0] = "red"; $color[1] = "green"; $color[2] = "blue"; $color[3] = "black"; $color[4] = "white"; I'd like my script to then echo them all together at random, using each only once... so I could get "redblackwhitegreenblue" or "blackgreenbluewhitered", but never "redgreenredblackblack". So far I can echo a random element from the array by counting the elements and then using using array_rand. And that's where I fall apart. Any help? ??? Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/ Share on other sites More sharing options...
HeaDmiLe Posted September 23, 2008 Share Posted September 23, 2008 maybe the shuffle (http://hr.php.net/manual/en/function.shuffle.php) function? Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-648967 Share on other sites More sharing options...
dogfighter Posted September 23, 2008 Author Share Posted September 23, 2008 Tried, but my preg_split uses PREG_SPLIT_OFFSET_CAPTURE which causes it to just echo "Array" over and over again. If I change it to PREG_SPLIT_NO_EMPTY it works fine, but then I lose the character(s) that I'm splitting by. Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649012 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 put your preg in before my comment in my code <?php //he was talking about using shuffle after you create the array w/ your preg_split_offset_capture $color[0] = "red"; $color[1] = "green"; $color[2] = "blue"; $color[3] = "black"; $color[4] = "white"; shuffle($color); $color1 = ""; foreach ($color as $value){ $color1 .= $value; } print $color1; Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649020 Share on other sites More sharing options...
kenrbnsn Posted September 23, 2008 Share Posted September 23, 2008 You can also use implode() and skip the foreach() <?php <?php //he was talking about using shuffle after you create the array w/ your preg_split_offset_capture $color[0] = "red"; $color[1] = "green"; $color[2] = "blue"; $color[3] = "black"; $color[4] = "white"; shuffle($color); echo implode('',$color); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649029 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 You can also use implode() and skip the foreach() <?php <?php //he was talking about using shuffle after you create the array w/ your preg_split_offset_capture $color[0] = "red"; $color[1] = "green"; $color[2] = "blue"; $color[3] = "black"; $color[4] = "white"; shuffle($color); echo implode('',$color); ?> Ken You're no fun Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649030 Share on other sites More sharing options...
dogfighter Posted September 24, 2008 Author Share Posted September 24, 2008 put your preg in before my comment in my code <?php //he was talking about using shuffle after you create the array w/ your preg_split_offset_capture $color[0] = "red"; $color[1] = "green"; $color[2] = "blue"; $color[3] = "black"; $color[4] = "white"; shuffle($color); $color1 = ""; foreach ($color as $value){ $color1 .= $value; } print $color1; Again, ArrayArrayArrayArray here's my full code: <?php $paragraph = "This is sentence one. And this is sentence two. Finally, sentence number three."; $sentsplit = preg_split("/[.!?]/", $paragraph, -1 , PREG_SPLIT_OFFSET_CAPTURE); shuffle($sentsplit); $sentsplit1 = ""; foreach ($sentsplit as $value){ $sentsplit1 .= $value; } print $sentsplit1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649156 Share on other sites More sharing options...
kenrbnsn Posted September 24, 2008 Share Posted September 24, 2008 With the option "PREG_SPLIT_OFFSET_CAPTURE" the preg_split returns a two-dimensional array. Leave that off and this code works: <?php $paragraph = "This is sentence one. And this is sentence two. Finally, sentence number three."; $sentsplit = array_map('trim',preg_split("/[.!?]/", $paragraph, -1)); shuffle($sentsplit); echo implode('',$sentsplit); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649164 Share on other sites More sharing options...
dogfighter Posted September 24, 2008 Author Share Posted September 24, 2008 With the option "PREG_SPLIT_OFFSET_CAPTURE" the preg_split returns a two-dimensional array. Leave that off and this code works: <?php $paragraph = "This is sentence one. And this is sentence two. Finally, sentence number three."; $sentsplit = array_map('trim',preg_split("/[.!?]/", $paragraph, -1)); shuffle($sentsplit); echo implode('',$sentsplit); ?> Ken That does work, thanks Ken. Reason I was using preg_offset_capture was because without it I lose the punctuation that I'm splitting by. So when it spits my sentences back out, I lose the period or question mark or exclamation point that ended the sentences in $paragraph. Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649165 Share on other sites More sharing options...
dogfighter Posted September 24, 2008 Author Share Posted September 24, 2008 Anything I can throw in there to preserve the ending punctuation the way preg_offset_capture did? Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649383 Share on other sites More sharing options...
kenrbnsn Posted September 24, 2008 Share Posted September 24, 2008 You were using the wrong flag and that threw me off. The flag you should be using is PREG_SPLIT_DELIM_CAPTURE. This should give you what I think you want: <?php $paragraph = "This is sentence one. And this is sentence two! Finally, sentence number three?"; $sentsplit = preg_split("/([.!?])/i", $paragraph, -1 , PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); $tmp = array(); for ($i=0;$i<count($sentsplit);$i+=2) $tmp[] = $sentsplit[$i] . $sentsplit[$i+1]; shuffle($tmp); echo implode('',$tmp); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649436 Share on other sites More sharing options...
Andy-H Posted September 24, 2008 Share Posted September 24, 2008 Ken, I think it was you that told me not to use a function in a loop... lol Dont suppose it loops many times tho:p Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649448 Share on other sites More sharing options...
kenrbnsn Posted September 24, 2008 Share Posted September 24, 2008 You mean using count() in a for loop? No, I wasn't the bearer of that tidbit. Ken Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649472 Share on other sites More sharing options...
dogfighter Posted September 24, 2008 Author Share Posted September 24, 2008 You were using the wrong flag and that threw me off. The flag you should be using is PREG_SPLIT_DELIM_CAPTURE. This should give you what I think you want: <?php $paragraph = "This is sentence one. And this is sentence two! Finally, sentence number three?"; $sentsplit = preg_split("/([.!?])/i", $paragraph, -1 , PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); $tmp = array(); for ($i=0;$i<count($sentsplit);$i+=2) $tmp[] = $sentsplit[$i] . $sentsplit[$i+1]; shuffle($tmp); echo implode('',$tmp); ?> Ken Worked like a charm! Doesn't split at the exclamation points (joins them with the next sentence) but I can probably work that one out. Thanks Ken (and everyone else!) for your help. Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649497 Share on other sites More sharing options...
dogfighter Posted September 24, 2008 Author Share Posted September 24, 2008 Yup, just escaped the exclamation point and it's-a-workin! Quote Link to comment https://forums.phpfreaks.com/topic/125519-solved-echo-array-elements-randomly-and-each-only-once/#findComment-649498 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.