Solved

if more images are called

Posted on 2011-09-17
9
274 Views
Last Modified: 2012-05-12
see related question

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





the code does that but if more images are called (2nd paramater) than are available only use available
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
Comment
Question by:rgb192
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:acbxyz
ID: 36554498
Between line 3 and 4, you can insert one more line to avoid an endless loop::
if ($img_num >= $img_array_len) return $img_arr;

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 36554508
>>but if more images are called (2nd paramater) than are available only use available
function chose($img_arr, $img_num) {
    if( $img_num >= count($img_arr) ){return $img_arr;}
    $rand_img = array();
...
}
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36554512
The code posted with this question will not work:

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

$imageSet is an array.  echo is unlikely to produce anything useful.

The correct technique is still the same as the one I posted in the other question, and as shown in this code snippet.  I'll leave it as an exercise for others to package this into a function.

Good luck with your project, ~Ray
<?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');

// UNIQUE NAMES ONLY
$images = array_unique($images);

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

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

// TRY TO GET THIRTEEN AT RANDOM
shuffle($images);
$chosen = array_slice($images , 0, 13);

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

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:rgb192
ID: 36555122
ray is correct also, I just dont know how to put in function


question for acbxyz, hielo


    if( $img_num >= count($img_arr) ){return $img_arr;}

if ($img_num >= $img_array_len) return $img_arr;


works but does not do random order
0
 
LVL 10

Expert Comment

by:acbxyz
ID: 36555149
Multiple possibilities:
1) between line 3 and 4
if ($img_num >= count($img_array_len)) {
  shuffle($img_arr);
  return $img_arr;
}

Open in new window

2) between 7 and 8
} else if (count($rand_img) == $img_array_len) {
  return $rand_img;
}

Open in new window

3) Based on Ray's advice, and I must admit, this would be my choice
function chose($img_arr, $img_num) {
    $arr2 = array_unique($img_arr); // side effect: be shure we don't have a by reference variable
    shuffle($arr2);
    return array_splice($arr2, 0, $img_num);
}

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36555165
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36555171

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

Author Closing Comment

by:rgb192
ID: 36556055
now this is the best answer.
thank you
0
 

Author Comment

by:rgb192
ID: 36565139
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 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