Hello, first time here... Apologize if I am out of protocol.

The following is, well, working, but failing:

<?php
$random = array(
	"test",
	"pass", 
	"fail",
	"works",
	"nope",
	"another",
	"shortaug",
	"stuck",
	"dumb"
	);

$current = "test";
$r1 = $random[$_GET['r1']];
$r2 = $random[$_GET['r2']];

function array_filter($array, $filter){ 
  $newArray = array (); 
  foreach($array as $object) 
    if(stristr($object, $filter)===false && strlen($object)>0) 
      $newArray[] = $object; 
  return $newArray; 
}
$random = array_delete($random, $current);
$random = array_delete($random, $r1);
$random = array_delete($random, $r2);

$rand = array_rand($random, 2);

$r1 = $random[$rand[0]]; 
$r2 = $random[$rand[1]]; 
?>

Now, a print_r($random) will show that the array has indeed been filtered and the index keys adjusted accordingly.

The problem is with $rand = array_rand($random,2);. It is acting as if the original array has not been filtered and regenerated.

IE: If $_GET['r1'] is equal to 'pass', then in theory it should not ever show up in either $r1 or $r2 once I run array_rand, but it is. The same for any value set as $r2. Now, the way I am using the script, I cannot tell if it is behaving in the same manner for $current, but I see no reason why it wouldn't be failing in that one too.

Any help?

    Uhm... [man]array_filter/man is already a function. What's more, you're calling some array_delete() function which doesn't exist. So, perhaps you meant to declare the array_delete() function here but... got confused?

    EDIT: Also, I don't know if it's "simpler", but it might be more efficient to reduce this:

    function array_filter($array, $filter){ 
      $newArray = array (); 
      foreach($array as $object) 
        if(stristr($object, $filter)===false && strlen($object)>0) 
          $newArray[] = $object; 
      return $newArray; 
    } 
    $random = array_delete($random, $current); 
    $random = array_delete($random, $r1); 
    $random = array_delete($random, $r2); 
    

    to this:

    function array_delete(&$array){
    	if(func_num_args() > 1) {
    		$filter = func_get_args();
    		array_shift($filter);
    		foreach($array as $key => $value) 
    			if(in_array($value, $filter) || $value == '')
    				unset($array[$key]);
    	}
    }
    
    array_delete($random, $current, $r1, $r2);

      For a reason (my absent-mindedness), your's wasn't working either, and so I playing around with another alternative, which was suffering from the same problem.

      So, I went and checked my filenames, and sure enough, I had one misnamed and that's why it was showing up.... renamed it and everything is working hunky-dorry.

      So... 3 ways to achieve this, and I believe the 3rd way is the fastest:

      3rd way:

      $used = array($current, $r1, $r2);
      $random = array_diff($random, $used);

      That's it....

        shortaug wrote:

        I believe the 3rd way is the fastest:

        Definitely seems like it... heh, now I feel stupid :p

          Write a Reply...