Hi.
I'm trying to write a code that selects 3 unique random arrays from a multiple array.
I've got this far:
<?php
$people = array(
array (
"Name" => "Chris",
"Strength" => 4,
"Business" => 3,
"Wages" => 2000),
array (
"Name" => "Bob",
"Strength" => 5,
"Business" => 2,
"Wages" => 1000),
array (
"Name" => "Kelly",
"Strength" => 3,
"Business" => 5,
"Wages" => 1500),
array (
"Name" => "Johnny",
"Strength" => 4,
"Business" => 2,
"Wages" => 1000),
array (
"Name" => "Tony",
"Strength" => 5,
"Business" => 5,
"Wages" => 2000),
array (
"Name" => "Jesse",
"Strength" => 1,
"Business" => 2,
"Wages" => 500),
);
// print_r($people);
for ($i=0; $i<=2; $i++) {
$random[$i] = array_rand($people);
//this generates the random number from the array
echo "<br> loop $i <br> $random[$i] call";
echo "<br> $random[$i]"; // printing result
$random = array_unique($random);
echo "deleting duplicates... <br />";
print_r($random);
if ($i == 2) {
// echo "Loop $i, No. ";
$result = count($random);
if ($result < 3) {
$i--;
continue;
} else {
echo $result;
}
}
}
echo "<br />Hire one of these 3 people. <br />";
?>
But occasionally it gets into an infinite loop, which I'd like to eliminate before I add more code.
All helpful suggestions are appreciated.
Thanks in advance.