0

I have this code to get 3 random values from my array:

$maps_clean = array_filter($find_league_maps);
$random_maps = array_rand($maps_clean,3);

$league_match_maps = ",".$maps_clean[1].",".$maps_clean[2].",".$maps_clean[3].",";

This works as long as the array has at least 3 values. Now I want to modify my code so that when I want more random values than I have in my array, it just gets new ones out of the array again. Yes, this means I can have some values more than once.

How would I do that?

5
  • What do you mean by iterate back over and add another value? Do you mean use a value just twice? Or do you mean a default value? ...? Commented Apr 7, 2016 at 0:34
  • Just to note: array_rand() returns an array full of random keys. You just access the array by its own and don't use $random_maps at all. Commented Apr 7, 2016 at 1:54
  • @Yes you want to fill the third, but with what? A default value? A value from the array, which then is just 2 times in the output? With what do you want to fill the third one? Commented Apr 7, 2016 at 2:00
  • 1
    A code snippet says more than a thousand words Commented Apr 7, 2016 at 2:02
  • My question still stands: If you have an array such as [5, 10] and you want 3 random values from it. With what do you want to fill the 3rd one? With a default value such as 100? Or with a value again out of the array, so you end up with [5, 10, 5 or 10]? Commented Apr 7, 2016 at 2:06

3 Answers 3

1

You can use a simple while loop and shuffle() the array inside of it, then get as many random elements as you need with array_slice(). Now if you want more random values than you have array elements, it simple takes the entire array, goes into the next iteration and takes the rest which it needs from the new shuffled array.

Code

<?php

    $arr = [1,2,3,4];


    $random = 5;
    $result = [];

    while(count($result) != $random){
        shuffle($arr);
        $result = array_merge($result, array_slice($arr, 0, $random - count($result))); 
    }

    print_r($result);

?>
Sign up to request clarification or add additional context in comments.

2 Comments

Since the OP seems to be using this for creating pairs, I would suggest pulling out the first two entries from the shuffled array to avoid having an entry paired with itself.
@Anthony I don't quite get what you mean with pairs? And I am pulling the first two entries from the shuffled array.
0

You can replace the elements having the same keys of an array fill with values with your $maps_clean.

$maps_clean = array_replace(array_fill(1, 3, null), array_filter($find_league_maps));

Here array_fill returns:

array(3) {
  [1]=>
  NULL
  [2]=>
  NULL
  [3]=>
  NULL
}

and its elements are replaced by the elements returned by array_filter($find_league_maps) that have the same keys.

2 Comments

Love this answer! I personally would extract the arguments to array_replace into variables for readability, but that array_fill function is a new one for me
You want to check OP's clarified question. OP wants to fill up the missing values with values from the array again.
0

The array key is start from 0 by default so, try with

 $league_match_maps = ",".$maps_clean[0].",".$maps_clean[1].",".$maps_clean[2].",";

also what is the output of count($maps_clean)?

1 Comment

You probably also want to look at this comment: stackoverflow.com/questions/36464573/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.