Solved

chose random images for a banner

Posted on 2011-09-12
6
261 Views
Last Modified: 2012-05-12

want to chose random images for a banner  with no duplicate images



function chose(image inventory, number of images to pack in return array){}


$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif');
$imageSet = chose($images, 4);
echo $imageSet
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Expert Comment

by:santoshmotwani
ID: 36527458
0
 
LVL 8

Accepted Solution

by:
kivan24 earned 500 total points
ID: 36527846
try this:
function chose($img_arr, $img_num) {
    $rand_img = array();
    $img_array_len = count($img_arr);
    for ($i=0; $i<$img_num; $i++) {
        $randindex = rand(0, $img_array_len-1);
        if (!in_array($img_arr[$randindex], $rand_img)) {
            $rand_img[] = $img_arr[$randindex];
        } else {
            $i--;
        }
    }
    return $rand_img;
}


$images = array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg', '7.jpg', '8.gif');
$imageSet = chose($images, 4);
print_r ($imageSet);

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36528541
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36528576
Refresh this screen a few times...
http://www.laprbass.com/RAY_temp_rgb192.php

If you have the risk of duplicates in the original $images array, you can use
http://us.php.net/manual/en/function.array-unique.php

Best regards, ~Ray
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

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

// GET FOUR AT RANDOM
shuffle($images);
$chosen = array_slice($images , 0, 4);

// SHOW THE WORK PRODUCT
print_r($chosen);

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 36554451
this handles duplicates.
thanks
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36554500
Your choice, of course, but I think I would rather use two statements instead of thirteen.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

690 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