Solved

use some images more often than others

Posted on 2011-09-18
6
269 Views
Last Modified: 2012-08-14
//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

0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Expert Comment

by:m4trix
ID: 36556592
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

0
 
LVL 3

Expert Comment

by:Duboux
ID: 36556664
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 ?
0
 

Author Comment

by:rgb192
ID: 36558021
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
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 7

Accepted Solution

by:
m4trix earned 500 total points
ID: 36558082
As I said before, you never specified what you were looking for. Nontheless, here is a version that does not repeat images:

<?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 UP TO $a1Cnt IMAGES FROM a1 TO THE RETURN ARRAY      
    shuffle($a1images);
    $returnArr = array_merge($returnArr, array_slice($a1images, 0, $a1Cnt));
    
    // ADD UP TO $a2Cnt IMAGES FROM a2 TO THE RETURN ARRAY      
    shuffle($a2images);
    $returnArr = array_merge($returnArr, array_slice($a2images, 0, $a2Cnt));
    
    // SHUFFLE THE RESULTANT ARRAY AND RETURN
    shuffle($returnArr);
    return $returnArr;
}
?>

Open in new window

0
 
LVL 3

Expert Comment

by:Duboux
ID: 36559118
Before I put any more time into this, I will wait for a detailed description of what you really need.
0
 

Author Closing Comment

by:rgb192
ID: 36565095
thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php construct 5 27
Dynamic varibles 5 34
AWS EC2 & RDS Instance 5 36
ebay api to see some data about my customers 2 31
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question