Jump to content

get random from array and remove duplicates


canabatz

Recommended Posts

Hi guys!

 

i got this code:

<?
$x = "abcde";

str_split($x);
for ($k=0; $k<=119; $k++){
$ax = array($x[0],$x[1],$x[2],$x[3],$x[4]);
shuffle($ax);
    $text = implode("", $ax) . "";
echo $text;
echo "<br />";
}
?>

 

if you test it you see that it is working but giving me allot of duplicates results!

 

there is a way to ignore duplicaes while it is runing?

 

thanx.

 

 

 

Link to comment
Share on other sites

<?php
$x='abcde';
str_split($x);
$array=array();
for($k=0; $k<=119; $k++){
$ax = array($x[0],$x[1],$x[2],$x[3],$x[4]);
shuffle($ax);
$text=implode("", $ax) . "";
$dupe=0;
foreach($array AS $row){
	if($text==$row){
		$dupe=1;
		break;
	}
}
if($dupe==0){
	echo $text.'<br />';
	$array[]=$text;
}
}
?>

 

I think this would work!

Also just to point it out, I think it can be written in 120 different ways. 5! (1*2*3*4*5)

so potentially they could all be different!

Link to comment
Share on other sites

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.