Jump to content

[SOLVED] Echo array elements randomly, and each only once?


Recommended Posts

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?  ???

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.

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;

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 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  :P

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;
?>

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

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.

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

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.  ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.