Solved

PHP exclude from array

Posted on 2010-09-19
13
760 Views
Last Modified: 2012-05-10
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.

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

      ?>

Open in new window

0
Comment
Question by:becraig
[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
  • Learn & ask questions
  • 6
  • 4
13 Comments
 
LVL 29

Author Comment

by:becraig
ID: 33713192
to add what I am trying to do is to ensure when I assign
  $j = rand(0, $i-1);


that I can exclude some numbers already assigned previously do not apply here.
0
 
LVL 4

Expert Comment

by:upandrun3
ID: 33713260
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
0
 
LVL 29

Author Comment

by:becraig
ID: 33713338
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);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:upandrun3
ID: 33713431
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['used'])) 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
0
 
LVL 29

Author Comment

by:becraig
ID: 33713435
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 ?
0
 
LVL 4

Expert Comment

by:upandrun3
ID: 33713446
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
0
 
LVL 29

Author Comment

by:becraig
ID: 33759383
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.
0
 
LVL 29

Author Comment

by:becraig
ID: 33761702
I really need some help here can someone show me a method by which I can do a rand range excluding predetermined numbers  ?
0
 
LVL 4

Accepted Solution

by:
upandrun3 earned 500 total points
ID: 33761970
Hi becraig,

This is based on what I suggested before, but a bit more complete. Throw it into a PHP file and then test through your browser to see how it works. You should be able to adapt this for your solution.

Pete


<?php
session_start();
if (isset($_GET) && $_GET['reset'] == 1) session_destroy();

$i = 5;

while(1) {  // always loop
    $j = rand(0, $i-1);
    if (!isset($_SESSION['used']) || !in_array($j,$_SESSION['used']) || count($_SESSION['used']) >= $i ) break;
}

if (!isset($_SESSION['used'])) $_SESSION['used'] = array();
$_SESSION['used'][] = $j;

if (count($_SESSION['used']) >= $i) echo "Used values equal the number of possible unique values. Please reset your session by passing ?reset=1.<br>\n";

echo "Value of j:  {$j}<br>\n";
echo "Values in used array:<br>\n";
echo "<pre>";
print_r($_SESSION['used']);
echo "</pre>";
0
 
LVL 29

Author Comment

by:becraig
ID: 33762022
Looks good testing now.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34387469
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.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

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…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

734 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