mercredi 28 novembre 2018

shuffle random pair in array but excluding pair possibility

I wish to create a small script to generate pair of users.

Users are in array so I use shuffle to pair them randomly.

My problem is I want to avoid / exclude some pair of users in the final result.

So I have the first part to shuffle users :

<?php 

$user[] = "Miaa";
$user[] = "Xavier";
$user[] = "Antoine";
$user[] = "Marie-Ange";
$user[] = "Claire";
$user[] = "Orlando";
$user[] = "Camille";
$user[] = "Chloé";
$user[] = "Audrey";
$user[] = "*";

$users = count($user);
// Shuffle user
shuffle($user);// You get a shuffled array

// Pair the adjacent user
for ( $index = 0; $index < $users; $index +=2) {

    // Pair $user[$index ] with $user[$index +1]
    echo $user[$index ] . " <=> " . $user[$index+1] . "\n";
}

?>

Here it's a link with my code into a sandbox => http://sandbox.onlinephpfunctions.com/code/6ef04e99946849606544493e64e317206209c10c

I tried to include if statment but without success

$user[$index[0]] == "Miaa" && $user[$index[1]] == "Xavier"




Aucun commentaire:

Enregistrer un commentaire