首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP -仅对数组的一部分进行混洗

PHP -仅对数组的一部分进行混洗
EN

Stack Overflow用户
提问于 2014-11-04 00:45:19
回答 3查看 1.7K关注 0票数 4

我有一个这样的数组:

代码语言:javascript
复制
$animals = array (
    'giraffe',
    'lion',
    'hippo',
    'dog',
    'cat',
    'rabbit',
    'fly',
    'hamster',
    'gerbil'
    'goldfish'
);

除了这两个2- hamstergerbil之外,这就是我想要的数组顺序

我想在哪一个先来之间随机选择。我知道我可以使用:

代码语言:javascript
复制
shuffle($animals);

将它们全部随机化,但我只想这样做2.所以如果我做一个print_r($animals),我可能会让仓鼠先于沙鼠,但另一次我会先于仓鼠

EN

回答 3

Stack Overflow用户

发布于 2015-02-13 09:18:19

添加Fisher-Yates-Knuth unbiased shuffling algorithm的2个变体,仅包含索引或排除索引(类似php的伪代码)

代码语言:javascript
复制
function shuffle_include( $a, $inc ) 
{
    // $a is array to shuffle
    // $inc is array of indices to be included only in the shuffle
    // all other elements/indices will remain unaltered

    // fisher-yates-knuth shuffle variation O(n)
    $N = count($inc);
    while ( $N-- )
    { 
        $perm = rnd( 0, $N ); 
        $swap = $a[ $inc[$N] ]; 
        $a[ $inc[$N] ] = a[ $inc[$perm] ]; 
        $a[ $inc[$perm] ] = $swap; 
    }
    // in-place
    return $a;
}

function shuffle_exclude( $a, $exc ) 
{
    // $a is array to shuffle
    // $exc is array of indices to be excluded from the shuffle
    // all other elements/indices will be shuffled
    // assumed excluded indices are given in ascending order
    $inc = array();
    $i=0; $j=0; $l = count($a); $le = count($exc)
    while ($i < $l)
    {
        if ($j >= $le || $i<$exc[$j]) $inc[] = $i;
        else $j++;
        $i++;
    }
    // rest is same as shuffle_include function above

    // fisher-yates-knuth shuffle variation O(n)
    $N = count($inc);
    while ( $N-- )
    { 
        $perm = rnd( 0, $N ); 
        $swap = $a[ $inc[$N] ]; 
        $a[ $inc[$N] ] = $a[ $inc[$perm] ]; 
        $a[ $inc[$perm] ] = $swap; 
    }
    // in-place
    return $a;
}

示例:

代码语言:javascript
复制
$a = array(1,2,3,4,5,6);

print_r( shuffle_include( $a, array(0,1,2) ) );
// sample output: [2,1,3,4,5,6] , only 0,1,2 indices are shuffled

print_r( shuffle_exclude( $a, array(0,1,2) ) );
// sample output: [1,2,3,6,5,4], all other indices are shuffled except 0,1,2

注意到的混洗函数本身使用了一种变种的Fisher-Yates-Knuth混洗算法

NOTE2给定的所有置乱算法(以及PHP的原始置乱函数)的(平均)时间复杂度为$O(n)$ (要置乱的数组的n=size)

有关shuffle的其他变体,请参阅:

  1. Efficiently pick n random elements from PHP array (without shuffle)
票数 1
EN

Stack Overflow用户

发布于 2014-11-04 02:32:54

代码语言:javascript
复制
$animals = array (
'giraffe',
'lion',
'hippo',
'dog',
'cat',
'rabbit',
'fly',
'goldfish'
);
$other = array('hamster','gerbil');
$allAnimals = array();
foreach($animals as $key => $animal){
    if($key == 7){
        $allAnimals = array_merge($allAnimals,shuffle($other));
    }
$allAnimals[] = $animal;
}
票数 0
EN

Stack Overflow用户

发布于 2020-10-24 03:44:15

代码语言:javascript
复制
function shuffle_include($a, $include_indexes)    
{    
    $b = array();    
    foreach ($include_indexes as $i => $v)    
        $b[] = $a[$v];    
            
    shuffle($b);    
    
    foreach ($include_indexes as $i => $v)    
        $a[$v] = $b[$i];    
     
    return $a;    
}

示例:

代码语言:javascript
复制
$animals = array (
'giraffe',
'lion',
'hippo',
'dog',
'cat',
'rabbit',
'fly',
'hamster',
'gerbil',
'goldfish');

$new_animals = shuffle_include($animals, array(7,8));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26718843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档