Solved

if more images are called

Posted on 2011-09-17
9
273 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 dynamically set the form action using jQuery.

777 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