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.

 

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.