【问题标题】:Select and delete random keys from multidimensional array从多维数组中选择和删除随机键
【发布时间】:2015-10-13 13:41:55
【问题描述】:

我有一个随机性问题。 我有示例数组。

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

我想从 $example 中选择随机指定的变量数量 ($amount),但当然 - 总是来自其他组。

示例结果:

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

到目前为止我尝试了什么?

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]
}
}

我现在不知道要解决什么问题或下一步该做什么。

感谢您的帮助!

【问题讨论】:

    标签: php arrays random


    【解决方案1】:
    $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);
    

    测试结果:

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

    【讨论】:

    • 它不起作用。在第一个数组中,测试 4 和 3 来自同一组 - F2。
    • @user2184072 查看编辑后的版本
    • 看起来很棒!感谢帮助。 :)
    • 是否有可能阻止现在不会使用的组?在表格 1. 随机 - 不是来自 F2 组 2. 随机 - 不是来自 F3 组 3. 随机 - 不是来自 F5 组 4. 随机 - 不是来自 F1 组 我得到了要在数组中阻止的值('F2','F3', 'F5','F1')。
    • @user2184072 你能说得更清楚一些吗?请一个听不清你在说什么。
    猜你喜欢
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2013-11-05
    • 2018-06-07
    • 1970-01-01
    相关资源
    最近更新 更多