2

I have one problem with randomness. I have example array.

$example=array(
    'F1' => array('test','test1','test2','test5'),
    'F2' => array('test3', 'test4'),
    'F3' => array('one','twoo','threee','x'),
    'F5' => array('wow')
)

I want to choose random keys from array to other array with specified size. In this second array I want values from other groups.

For example i got

  $amounts = array(4,3,1,2,1);

I want to choose randomly specified ammount of variables ($amount) from $example, but of course - always from other groups.

Example result:

   $result=(
array(4) => ('test','test4','x','wow'),
array(3) => ('test2','test3','three'),
array(1) => ('test1')
array(2) => ('test5','one')
array(1) => ('twoo')

What I tried so far?

foreach($amounts as $key=>$amount){
   $random_key[$key]=array_rand($example,$amount);
   foreach($result[$key] as $key2=>$end){
    $todelete=array_rand($example[$end]);
    $result[$key][$key2]=$example[$amount][$todelete]
}
}

I don't know now, what to fix or to do next.

Thanks for help!

1 Answer 1

2
$example = array(
    'F1' => array('test', 'test1', 'test2', 'test5'),
    'F2' => array('test3', 'test4'),
    'F3' => array('one', 'twoo', 'threee', 'x'),
    'F5' => array('wow')
);
$amounts = array(4, 3, 1, 2, 1);

$result = array();

$example = array_values($example);
//randomize the array
shuffle($example);
foreach ($example as $group) {
    shuffle($group);
}

//sort the example array by child length
usort($example, function ($a, $b) {
    return count($b) - count($a);
});

foreach ($amounts as $amount) {
    $tmpResult = array();
    for ($i = 0; $i < $amount; $i++) {
        if(empty($example[$i])){
            throw new \InvalidArgumentException('The total number of values in the array exceed the amount inputed');
        }
        $tmpResult[] = array_pop($example[$i]);
    }

    $result[] = $tmpResult;

    //sort the example array again by child length
    usort($example, function ($a, $b) {
        return count($b) - count($a);
    });
}

print_r($result);

test result:

Array
(
    [0] => Array
    (
        [0] => test5
        [1] => x
        [2] => test4
        [3] => wow
    )
    [1] => Array
    (
        [0] => test2
        [1] => threee
        [2] => test3
    )
    [2] => Array
    (
        [0] => test1
    )
    [3] => Array
    (
        [0] => twoo
        [1] => test
    )
    [4] => Array
    (
        [0] => one
    )
)
Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work. In first array test 4 and 3 are from the same group - F2.
@user2184072 see the Edited version
Looks great! Thanks for help. :)
Is there any possibility to block group which wouldn't be used right now? In form 1. random - not from group F2 2. random - not from group F3 3. random - not from group F5 4. random - not from group F1 I got values to block in array('F2','F3','F5','F1').
@user2184072 can you be more clear please a can't find out what you are saying.

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.