1

Basically, I need to add several random items from a PHP array to a choice that a user makes from that array. So for example, if the array is:

"kiwi, orange, pineapple, apple, grape, starfruit, kumquat"

and the user picks "pineapple" I want to choose X number of additional fruits from the remaining array items. The key thing is that "pineapple" can't be both the selection and also one of the additional fruits, so it needs to be excluded from the array once it's chosen as the selection.

Selection: pineapple
Your additional fruits:  kiwi, grape, orange

NOT Selection: pineapple Your additional fruits: kiwi, pineapple, grape

I'm actually doing this with filenames, not fruits, but it seems easier to describe this way.

I think I can do the random selection part, but I'm not sure how to remove the item that's selected from the given array in PHP. Thanks very much for any suggestions or ideas.

Dave
  • 11
  • 1
  • This is similar to [http://stackoverflow.com/questions/2448964/php-how-to-remove-specific-element-from-an-array](http://stackoverflow.com/questions/2448964/php-how-to-remove-specific-element-from-an-array) – Steve Apr 05 '11 at 16:23

4 Answers4

3

If you don't know the exact position of the chosen item beforehand, use array_search() to find its index. unset() it from the array, then do your random selection.

Example

$key = array_search('pineapple', $fruits);
unset($fruits[$key]);

// Random selection here
simshaun
  • 21,263
  • 1
  • 57
  • 73
0

If you know the index of pineapple, you could just unset it. This removes it from the array: unset($array[$index]);

Christopher Armstrong
  • 7,907
  • 2
  • 26
  • 28
0

You can remove elements (which are referenced using an integer eg $Array[5]) by using array_splice

array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement ]] )

http://php.net/manual/en/function.array-splice.php

You can mix up an array using shuffle

bool shuffle ( array &$array )

http://php.net/manual/en/function.shuffle.php

This should do the job!

Adam Casey
  • 1,600
  • 12
  • 21
  • I think the problem with this approach is that it will be hard to identify the element. It could be at any place in the array and the array will vary. But the below option (karim79's) seems like it would work based on very little other than just knowing the person's selection. But thank you!! – Dave Apr 05 '11 at 17:14
  • I was assuming you knew the index of the item removed from the array, sorry about that – Adam Casey Apr 05 '11 at 17:16
0

Another approach would be to use array_diff to directly return an array which does not contain the chosen element(s), e.g.:

$theFruit = 'pineapple';
$remainder = array_diff($arr, array($theFruit));

//shuffle
shuffle($remainder);

// now do something with the first three from the shuffled remainder
print_r(array_slice($remainder, 0, 3));
karim79
  • 339,989
  • 67
  • 413
  • 406
  • This seems very simple and very much like what I was hoping to do! I didn't realize the array_diff option was there! Thank you! Now I just have to see if it'll actually work! ^^ – Dave Apr 05 '11 at 17:15