PHP - 只是数组的一部分

时间:2014-11-03 16:45:19

标签: php arrays shuffle

我有一个数组:

$animals = array (
    'giraffe',
    'lion',
    'hippo',
    'dog',
    'cat',
    'rabbit',
    'fly',
    'hamster',
    'gerbil'
    'goldfish'
);

这就是我希望数组与这些数组分开的顺序 - hamstergerbil

我想在哪一个出现之间随机化。我知道我可以使用:

shuffle($animals);

随便给他们随机,但我只想做这些2.所以如果我要做一个print_r($animals)我可能会让仓鼠来到沙鼠之前但是另一次在仓鼠之前得到沙鼠

4 个答案:

答案 0 :(得分:4)

你可以splice数组来获取元素,随机化它们的顺序并将它们放回原始数组中:

$sub = array_splice($animals, 7, 2);
shuffle($sub);
array_splice($animals, 7,0, $sub);
var_dump($animals);

答案 1 :(得分:1)

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

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

示例:

$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

注意 PHP的shuffle函数本身使用了Fisher-Yates-Knuth混洗算法的变体

NOTE2 给出的所有shuffle算法(以及PHP的原始shuffle函数)的(平均)时间复杂度为$ O(n)$(n =要混洗的数组的大小)< / p>

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

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

示例:

$animals = array (
'giraffe',
'lion',
'hippo',
'dog',
'cat',
'rabbit',
'fly',
'hamster',
'gerbil',
'goldfish');

$new_animals = shuffle_include($animals, array(7,8));