Solved

# chose random images for a banner

Posted on 2011-09-12
260 Views

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
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

LVL 16

Expert Comment

ID: 36527458
0

LVL 8

Accepted Solution

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);
0

LVL 110

Expert Comment

ID: 36528541
0

LVL 110

Expert Comment

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);
0

Author Closing Comment

ID: 36554451
this handles duplicates.
thanks
0

LVL 110

Expert Comment

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

## Featured Post

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
###### Suggested Courses
Course of the Month7 days, 23 hours left to enroll