samedi 17 septembre 2016

How to generate randomized pairs of elements in php without duplicates?

I'm trying to generate randomized pairs of elements:

<? 
$myarray1=array('1','2','3'); 
$myarray2=array('a','b','c'); 

$key_rand = array_rand($myarray1);

echo '<p>'.$myarray1[$key_rand].'</p>'; 
echo '<p>'.$myarray2[$key_rand].'</p>'; 


$myarray1=array('1','2','3'); 
$myarray2=array('a','b','c'); 

$key_rand = array_rand($myarray1);

echo '<p>'.$myarray1[$key_rand].'</p>'; 
echo '<p>'.$myarray2[$key_rand].'</p>'; 


$myarray1=array('1','2','3'); 
$myarray2=array('a','b','c'); 

$key_rand = array_rand($myarray1);

echo '<p>'.$myarray1[$key_rand].'</p>'; 
echo '<p>'.$myarray2[$key_rand].'</p>'; 
?>

as a result I sometimes get duplicated pairs, which I don't want - for example:

2b 2b 3c

or

1a 1a 1a

I want to get only unique pairs each time, for example:

2b 1a 3c

or

1a 3c 2b

etc.

I'm probably doing it wrong, how do I fix that? Sorry new to this




Aucun commentaire:

Enregistrer un commentaire