Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Madness!

Status
Not open for further replies.

SoyDelicious

Technical User
Jul 23, 2002
46
0
0
US
Objective:

Create a array from files in directory, then use array_rand() to randomly pick one of these files, of which will be used to reflect an image onto our CMS. While doing so I need to filter the array for listings like "." or "..". Presently it does indeed randomize the output although it doesn't pick one, which is very disaapointing. Thanks for your time. If someone does have a second to help if you could be as verbose as possible so that I can learn from this.

Below is code.

//handle checkin that shit out
$handle = opendir('./classes');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
$count = count($files);
srand((float) microtime() * 10000000);
$rand_keys = array_rand($files, $count);
print $files[$rand_keys[0]] . "\n";
}
closedir($handle);

 
just a quick note to make your code quicker/easier to read:

Code:
while (false !== ($file = readdir($handle)))
can be simplified to
Code:
while (($file = readdir($handle)) == True)
and then to
Code:
while ($file = readdir($handle))

anyway, I haven't had a lot of experience with the sort of thing you're doing, and I can't see any obvious mistakes, so I'll let someone else figure it out ;)
 
Filtering for "." and ".." is a simple as wrapping your array-insert line with an if-statement that doesn't perform the insert for those directory entries.

If you're trying to just pull out one, don't hand array_ran() the second parameter. The way you're invoking the funciton, it's returning the entire array, just in random order.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks both of you for your help. When I clear the second variable it ceases to print anything. Let me know if you have any idea's, thanks.
 
Be aware of the difference in array_rand when only one element is returned:
The function passes back the randomly picked key, not the array element's value. You need to use
Code:
$myRand = $files[array_rand($files)];

Hope that helps.
 
I figured it out...

It was a problem with the placement of the array_rand statement. It had to come after the enclosed:

$handle = opendir('./classes');
while (false !== ($file = readdir($handle)))
{
$files[] = $file;
}

I tried it out before but it still included the $count parameter. Thanks for your help guys. Also parameter 1 will also work.

Final Code:

<?
mt_srand((double) microtime() * 1000000);
$handle = opendir('./classes');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
$randk = array_rand($files);
$random = $files[$randk];
print_r ($random);
closedir($handle);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top