Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

use some images more often than others

//a set of images that are used ($a1/($a1+$a2))% of the time
$a1Images = array('weight' => $a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));

//a set of images that are used ($a2/($a1+$a2))% of the time
$a2Images = array('weight' => $a2, 'images' => array('7.jpg', '8.gif', '8.gif'));


echo '<pre>' . print_r(array($a1Images, $a2Images), $NumberOfTotalImages), true) . '</pre>';
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif', '8.gif');

// SHOW HOW TO USE THE FUNCTION
print_r(image_array($images));
print_r(image_array($images, 4));
print_r(image_array($images, 713));

// DEFINE THE FUNCTION
function image_array($images, $num=1)
{
    // UNIQUE NAMES ONLY
    $images = array_unique($images);

    // GET A RANDOM COLLECTION
    shuffle($images);
    return array_slice($images, 0, $num);
}

Open in new window

Avatar of m4trix
m4trix
Flag of Canada image

You didn't actually ask a question here, but I assume you're looking to modify the code you have to weight images towards one of the two image arrays based on your weightings. The code below will do that. Note that since you didn't specify otherwise, images will be re-used if the list of images to use is shorter than required. In the future, please be clear about what you are actually asking for.

Here is how it works (based on wanting an array of 10 images with 80% from a1 [implying 8 images from a1, and 2 images from a2]):
1) if a1 has 10 images and a2 has 10 images, then 8 will be randomly selected from a1, and 2 will be randomly selected from a2, and the resulting array will be randomized and returned
2) if a1 has 5 images and a2 has 10 images, then all 5 images from a1 will be selected, then 3 more images randomly selected from a1, and 2 images will be randomly selected from a2. The resultant array will have 3 duplicates from a1

<?php
error_reporting(E_ALL);

// SET PERCENTAGES (80% a1 in this case)             
$a1 = 80;
$a2 = 20;

//a set of images that are used ($a1/($a1+$a2))% of the time
$a1Images = array('weight' => $a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
//a set of images that are used ($a2/($a1+$a2))% of the time
$a2Images = array('weight' => $a2, 'images' => array('7.jpg', '8.gif', '8.gif')); 

echo '<pre>';
// SHOW HOW TO USE THE FUNCTION
print_r(weighted_image_array($a1Images, $a2Images, 10));
echo "</pre>"; 
   
// DEFINE THE FUNCTION
function weighted_image_array($a1Images, $a2Images, $num=1)
{
    // UNIQUE NAMES ONLY
    $a1images = array_unique($a1Images['images']);
    $a2images = array_unique($a2Images['images']);
    
    // # OF IMAGES FROM EACH COLLECTION
    $a1Cnt = round($a1Images['weight']/($a1Images['weight']+$a2Images['weight']) * $num);
    $a2Cnt = $num - $a1Cnt;

    // GET A RANDOM COLLECTION
    $returnArr = array();
    
    // ADD a1 IMAGES (RANDOMLY) UNTIL THE REQUIRED AMOUNT ($a1Cnt) IS MET. IF THE # OF IMAGES IN a1 < $a1Cnt, THEN IT WILL DUPLICATE         
    $a1Total = 0;
    while ($a1Total < $a1Cnt) {
        shuffle($a1images);
        $returnArr = array_merge($returnArr, array_slice($a1images, 0, min($a1Cnt - $a1Total, count($a1images))));
        $a1Total += min($a1Cnt, count($a1images));
    }
    
    // ADD a2 IMAGES (RANDOMLY) UNTIL THE REQUIRED AMOUNT ($a2Cnt) IS MET. IF THE # OF IMAGES IN a2 < $a2Cnt, THEN IT WILL DUPLICATE     
    $a2Total = 0;
    while ($a2Total < $a2Cnt) {
        shuffle($a2images);
        $returnArr = array_merge($returnArr, array_slice($a2images, 0, min($a2Cnt - $a2Total, count($a2images))));
        $a2Total += min($a2Cnt, count($a2images));
    }
    
    // SHUFFLE THE RESULTANT ARRAY AND RETURN
    shuffle($returnArr);
    return $returnArr;
}
?>

Open in new window

Avatar of Duboux
Duboux

I don't see your question anyway, so I'm assuming u have an array of image names of which you want some to show up more than others, during a random show ?
Maybe even some twice more than others XD

okay, multi-array:


<?php

$a1 = 1;
$a2 = 2;
$a3 = 3;

// Create readable image array ;)
$arrayImages = array();
$arrayImages[] = array(
                        "weight" => $a1,
                        "images" => array("1.gif", "2.gif", "3.jpg", "4.jpg", "5.gif", "6.jpg")
);
$arrayImages[] = array(
                        "weight" => $a2,
                        "images" => array("7.gif", "8.gif", "9.jpg")
);
$arrayImages[] = array(
                        "weight" => $a3,
                        "images" => array("10.gif", "11.gif", "12.jpg")
);


// Fill the jar...
$ImageJar = array();      /// create empty jar...
foreach ($arrayImages as $I) {      /// go through all the $arrayImages[] lines
    $amount = $I["weight"];
    if (!is_integer($amount)) { exit("Weight must be an integer..."); }
    for ($i = 1; $i <= $amount; $i++) {      /// Add this line of images to the Jar.. And keep repeating till it's done $amount times...
        $ImageJar = array_merge($ImageJar, $I["images"]);
    }
}

// Pic a random image...
$picImg = rand(0,count($ImageJar) - 1);
echo "The randomly choosen pic = ".$ImageJar[$picImg];
echo "<br />Here's the Jars contents:<br />". nl2br(print_r($ImageJar, true));

?>

Open in new window


Is that what u mean ?
Avatar of rgb192

ASKER

m4trix
$a1Images = array('weight' => $a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
$a2Images = array('weight' => $a2, 'images' => array('7.jpg', '8.gif', '9.gif'));

the output images repeat.  I do not want repeats.
If there are more images called than available, then just display all


duboux
your code only finds one random image
ASKER CERTIFIED SOLUTION
Avatar of m4trix
m4trix
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Before I put any more time into this, I will wait for a detailed description of what you really need.
Avatar of rgb192

ASKER

thanks