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.

    Why not use count() to get the number of arrays you have, then use array_shuffle to mix them all up randomly and then finally select a number/key less than or equal to the count value?

      You are already using array_rand(), so why not provide it with a second argument of 3?

        I did use

        array_rand($people, 3)

        first time out, but the 3 results weren't unique. Thank you laserlight; do you know of a way to make it pick 3 unique results?
        Or will I have to use an if condition with array_unique?

        Thank you kbc1, I think I'll try that way too.

          // print_r($people);
          
          $random = array_rand($people, 3);
          //this generates the random 3 people
          
          $random = array_unique($random);
          // deleting duplicates
          print_r($random);
          
          
          echo "<br />Hire one of these 3 people. <br />";
          

          works... it selects 3 unique values but doesn't copy the multiple array :$

          At least the code's shorter.. now to try and work out how to copy a multiple array...

          Thank you

            <?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),
            
            );
            
            
            $random = array_rand($people, 3);
            //this generates the random 3 people
            
            $random = array_unique($random);
            // deleting duplicates
            
            foreach ($random as $k => $v) {
            	$display = $random[$k];
            	$random[$k] = $people[$display];
            
            }
            echo "<h3>Hire one of these 3 people.</h3> <br />";
            $a = 0;
            while ($a < 3) {
            
            echo "
            <div id='Pusher".$a."'>
            	<form name='Hire".$a."' action='index.php' method='POST'>
            		<fieldset>
            			<strong>Name</strong>
            			<input type='hidden' name='Name' value='".$random[$a]['Name']."' />"
            			.$random[$a]['Name']."
            			<br /><strong>Strength</strong>
            			<input type='hidden' name='Strength' value='".$random[$a]['Strength']."' />"
            			.$random[$a]['Strength']."
            
            			<br /><strong>Business</strong>
            			<input type='hidden' name='Business' value='".$random[$a]['Business']."' />"
            			.$random[$a]['Business']."
            
            			<br /><strong>Wages</strong>
            			<input type='hidden' name='Wages' value='".$random[$a]['Wages']."' />
            				".$random[$a]['Wages']."
            
            			<br /><input type='submit' value='Hire' />
            		</fieldset>
            	</form>
            </div>";
            $a++;
            
            }
            
            if (array_key_exists('Name', $_POST)) {
            	$hired[] = $_POST;
            	print_r($hired);
            }
            
            
            ?>
            
            

            I did use a for loop at first, but every for loop kept getting into an infinite loop (probably a bug in my server, which I have now updated).

            Thank you!

              Write a Reply...