becraig
asked on
PHP exclude from array
I want to be able to exclude a number from an array based on the fact the number has already been assigned.
the concept here is I am doing a random file include serveral times in one page.
I want to ensure that the same file is not included twice.
the concept here is I am doing a random file include serveral times in one page.
I want to ensure that the same file is not included twice.
<?php
$path = "/home/folder/";
$narray=array();
$dir_handle = @opendir($path) or die("Unable to open $path please notify the administrator. Thank you.");
$i=0;
while($file = readdir($dir_handle))
{
if($file != '.' && $file != '..' && $file !=' ' && $file !="SESSION['incl1']")
{
$narray[$i]=$file;
$i++;
}
}
//I want to make sure when I assign the value below it is not repeated from another instance of this script embedded in the php page
$j = rand(0, $i-1);
include ("$path$narray[$j]");
//closing the directory
closedir($dir_handle);
?>
Hi becraig,
Would this be in the context of the same running script or would the number need to be excluded from all other invocations of the script?
User1 calls the script, gets a value of say 10
User2 calls the script, he/she shouldn't get a value of 10.
Pete
Would this be in the context of the same running script or would the number need to be excluded from all other invocations of the script?
User1 calls the script, gets a value of say 10
User2 calls the script, he/she shouldn't get a value of 10.
Pete
ASKER
I run the same script 4 times, I set the current assigned number as a session variable and read it on the next iteration of the script:
ie: one the first invocation the value is assigned "5"
I want now to ensure on the second invocation 5 is excluded from $j = rand(0, $i-1);
ie: one the first invocation the value is assigned "5"
I want now to ensure on the second invocation 5 is excluded from $j = rand(0, $i-1);
how about something like this:
where this is in the script:
$j = rand(0, $i-1);
replace with this:
while(1) { // always loop
$j = rand(0, $i-1);
if (!isset($_SESSION['used']) || !in_array($j,$_SESSION['us ed'])) break; // if you haven't set the used array yet or if $j isn't in the used array, then break the loop, using the value of $j last retrieved
}
if (!isset($_SESSION['used']) ) $_SESSION['used'] = array(); // set up the array if it's not
$_SESSION['used'][] = $j
Note this may not be all that efficient if you have lots and lots of files and you run this a number of times close to the number of files.
Note, I didn't get a chance to test this out though, so it may require some tweaking. The comments should help with an approach on the logic.
Pete
where this is in the script:
$j = rand(0, $i-1);
replace with this:
while(1) { // always loop
$j = rand(0, $i-1);
if (!isset($_SESSION['used'])
}
if (!isset($_SESSION['used'])
$_SESSION['used'][] = $j
Note this may not be all that efficient if you have lots and lots of files and you run this a number of times close to the number of files.
Note, I didn't get a chance to test this out though, so it may require some tweaking. The comments should help with an approach on the logic.
Pete
ASKER
oK I think I found a way.
I added a while loop before for the include
$j = rand(0, $i-1);
while ($j !="val1" && $j !="val2")
{
include ("$path$narray[$j]");
}
Does this seem to make sense ?
I added a while loop before for the include
$j = rand(0, $i-1);
while ($j !="val1" && $j !="val2")
{
include ("$path$narray[$j]");
}
Does this seem to make sense ?
That may work, though you'll want to do a test that scales with the number of iterations you'd run of the script (based on what you described you need). Also, this snippet of code would have the possibility of getting into an infinite loop, unless you put the $j generation inside the loop. Also, I'm guessing you'll want to make sure that the include is only run once, right? If you put the $j generation inside the loop, you'll also need a condition to control when the include happens.
Pete
Pete
ASKER
OK I am really getting nowhere fast.
I really think I may need to find a way to exclude numbers from the random array, is there a way I can do that.
I really think I may need to find a way to exclude numbers from the random array, is there a way I can do that.
ASKER
I really need some help here can someone show me a method by which I can do a rand range excluding predetermined numbers ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Looks good testing now.
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
ASKER
$j = rand(0, $i-1);
that I can exclude some numbers already assigned previously do not apply here.