rgb192
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>';
$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);
}
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:
Is that what u mean ?
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));
?>
Is that what u mean ?
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
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Before I put any more time into this, I will wait for a detailed description of what you really need.
ASKER
thanks
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
Open in new window