* * * PHP random array with no duplicates

Discussion in 'PHP' started by mm-93, Sep 14, 2008.

  1. #1
    Can someone help me with the code for displaying images from a PHP array at random but with no duplicates?

    I really appreciate any help. thanks!
     
    mm-93, Sep 14, 2008 IP
  2. mNova

    mNova Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This may not be the most efficient way to do it, but it is the only way i knew how. If anyone knows a better way to do it, i would like to know it as well.

    
    <?php
    
        // Initialize the array
    	$imgArr = array(
    		'img1',
    		'img2',
    		'img3',
    		'img4',
    		'img5',
    		'img6',
    		'img7',
    		'img8',
    		'img9',
    	);
    
    	// Generate a random order
    	$order = array();
    	$max = count($imgArr) - 1;
    	for($i=0;$i<count($imgArr);$i++)
    	{
    		$order[] = uniqueRand($order, $max);
    	}
    
    	// Echo out the array using the random order
    
    	foreach($order as $index) {	echo $imgArr[$index]. "<br />"; }
    
    	//------------------------------------------
    	function uniqueRand(&$order, $max)
    	{
    		// Generate a random number
    		$i = rand(0, $max);
    
    		// Make sure the number hasn't been generated before.
    		while(in_array($i, $order))
    		{
    			$i = rand(0, $max);
    		}
    
    		// Return it
    		return $i;
    	}
    
    ?>
    
    PHP:
     
    mNova, Sep 14, 2008 IP
  3. mm-93

    mm-93 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Thanks a lot for the response! Here is what I ended up going with:

    
    <?php 
    $tempHolder = '1'; 
    $img_array = array('001c.jpg', '002.jpg', '003b.jpg', '004b.jpg', '005b.jpg', '007b.jpg', '008a.jpg', '010a.jpg', '011a.jpg', '012.jpg', '013.jpg', '014b.jpg', '015.jpg', '016d.jpg', '017a.jpg', '018b.jpg', '019b.jpg', '020b.jpg'); 
    shuffle($img_array); 
    echo '<table width="90%" align="center">'; 
    for($i = 0; $row_Recordset2 = mysql_fetch_assoc($Recordset2); $i++){ 
        echo <<<ROW 
        <tr> 
            <td align="center"> 
                <img src="http://{$row_Recordset2['Website']}/uploads/{$img_array[$i]}" /> 
            </td> 
        </tr> 
    ROW;
    } 
    echo '</table>'; 
    ?>
    
    PHP:
     
    mm-93, Sep 14, 2008 IP
  4. mm-93

    mm-93 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    
    
    $tempHolder = '1'; 
    $img_array = array('001c.jpg', 
    			'002.jpg', 
    			'003b.jpg', 
    			'004b.jpg', 
    			'005b.jpg', 
    			'007b.jpg', 
    			'008a.jpg', 
    			'010a.jpg', 
    			'011a.jpg', 
    			'012.jpg', 
    			'013.jpg', 
    			'014b.jpg', 
    			'015.jpg', 
    			'016d.jpg', 
    			'017a.jpg', 
    			'018b.jpg', 
    			'019b.jpg', 
    			'020b.jpg'); 
    shuffle($img_array); 
    echo "<table width=\"90%\" align=\"center\">"; 
    
    for($i = 0; $row_Recordset2 = mysql_fetch_assoc($Recordset2); $i++){ 
       
    echo    "<tr>";
    echo    "<td align=\"center\">";
    echo    "<a href=\"http://{$row_Recordset2['Website']}\" target=\"_blank\"><img src=\"http://{$row_Recordset2['Website']}/uploads/{$img_array[$i]}\" /></a>";
    echo    "</td>";
    echo    "</tr>";
    } 
    echo "</table>"; 
    
    
    
    
    
    PHP:


    Here's a question based off the code above.... I limited my SQL results to 18, however I actually have a lot more images that I need to display. I CAN have duplicates but just NOT together....

    So in other words, there are 100 domains and each of the 100 domains has a set of 18 images with the same name.

    I need to shuffle the array and display 18 images with no dups, now I need to display the next 18 with the image array shuffled again.. and then the next 18 etc...

    The net result would be that I can use the 18 images over and over again and yes there would be duplicates but none would be next to eachother (except in the unlikely event that the dups fall at the end and the beginning of the adjacent shuffled arrays which is no big deal).

    Does this make sense? Thanks again for your help!
    Mitch
     
    mm-93, Sep 14, 2008 IP
  5. mm-93

    mm-93 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here is what I ended up using:


    
    
    $tempHolder = '1'; 
    $img_array = array('001c.jpg', 
    			'002.jpg', 
    			'003b.jpg', 
    			'004b.jpg', 
    			'005b.jpg', 
    			'007b.jpg', 
    			'008a.jpg', 
    			'010a.jpg', 
    			'011a.jpg', 
    			'012.jpg', 
    			'013.jpg', 
    			'014b.jpg', 
    			'015.jpg', 
    			'016d.jpg', 
    			'017a.jpg', 
    			'018b.jpg', 
    			'019b.jpg', 
    			'020b.jpg'); 
    shuffle($img_array); 
    echo "<table width=\"90%\" align=\"center\">"; 
    
    for($i = 0; $row_Recordset2 = mysql_fetch_assoc($Recordset2); $i++){ 
       if($i == 18){ 
            $i = 0; 
        } 
    echo    "<tr>";
    echo    "<td align=\"center\">";
    echo    "<a href=\"http://{$row_Recordset2['Website']}\" target=\"_blank\"><img src=\"http://{$row_Recordset2['Website']}/uploads/{$img_array[$i]}\" /></a>";
    echo    "</td>";
    echo    "</tr>";
    } 
    echo "</table>"; 
    
    PHP:
    Did a check to see if $i == 18 and if so set it to 0 to start from the beginning of the shuffled array.
     
    mm-93, Sep 14, 2008 IP
  6. mNova

    mNova Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think i get what you are trying to do, just move that shuffle command inside the for loop. As far as i can tell, that should do what you want it to do.

    (And damn, i had no idea that shuffle function existed, thanks for that.)
     
    mNova, Sep 14, 2008 IP