PHP Random Shuffle Array维护密钥=>值

时间:2010-11-05 01:01:39

标签: php arrays

我一直在寻找谷歌的答案,但似乎无法找到一些万无一失的东西,并且无法将这一切搞砸(进入生产网站)。

我所拥有的是具有20多个过滤器的高级搜索,它返回包含ID和距离的数组。我需要做的是将这些结果随机播放,以便每次都以随机顺序显示。我现在出来的阵列是:

Array (
    [0] => Array ( [id] => 1 [distance] => 1.95124994507577 )
    [1] => Array ( [id] => 13 [distance] => 4.75358968511882 )
    [2] => Array ( [id] => 7 [distance] => 33.2223233233323 )
    [3] => Array ( [id] => 21 [distance] => 18.2155453552336 )
    [4] => Array ( [id] => 102 [distance] = 221.2212587899658 )
)

我需要做的是每次都随机或按顺序排列,但保持id和距离对,即:

Array (
    [4] => Array ( [id] => 102 [distance] = 221.2212587899658 )
    [1] => Array ( [id] => 13 [distance] => 4.75358968511882 )
    [3] => Array ( [id] => 21 [distance] => 18.2155453552336 )
    [2] => Array ( [id] => 7 [distance] => 33.2223233233323 )
    [0] => Array ( [id] => 1 [distance] => 1.95124994507577 )
)

谢谢:)

10 个答案:

答案 0 :(得分:83)

first user post文档下的shuffle

  

随机关联和   保留时的非关联数组   关键,价值对。也返回   洗牌阵列而不是洗牌   到位。

function shuffle_assoc($list) { 
  if (!is_array($list)) return $list; 

  $keys = array_keys($list); 
  shuffle($keys); 
  $random = array(); 
  foreach ($keys as $key) { 
    $random[$key] = $list[$key]; 
  }
  return $random; 
} 

测试用例:

$arr = array();
$arr[] = array('id' => 5, 'foo' => 'hello');
$arr[] = array('id' => 7, 'foo' => 'byebye');
$arr[] = array('id' => 9, 'foo' => 'foo');
print_r(shuffle_assoc($arr));
print_r(shuffle_assoc($arr));
print_r(shuffle_assoc($arr));

答案 1 :(得分:15)

从5.3.0开始,你可以这样做:

uksort($array, function() { return rand() > rand(); });

答案 2 :(得分:4)

在这里看看这个功能:

     $foo = array('A','B','C'); 


function shuffle_with_keys(&$array) {
    /* Auxiliary array to hold the new order */
    $aux = array();
    /* We work with an array of the keys */
    $keys = array_keys($array);
    /* We shuffle the keys */`enter code here`
    shuffle($keys);
    /* We iterate thru' the new order of the keys */
    foreach($keys as $key) {
      /* We insert the key, value pair in its new order */
      $aux[$key] = $array[$key];
      /* We remove the element from the old array to save memory */
      unset($array[$key]);
    }
    /* The auxiliary array with the new order overwrites the old variable */
    $array = $aux;
  }

      shuffle_with_keys($foo);
      var_dump($foo);

原帖在这里: http://us3.php.net/manual/en/function.shuffle.php#83007

答案 3 :(得分:3)

我在提供大部分答案时遇到了困难 - 所以我创建了这个小片段,它将我的阵列随机化,同时保留了他们的键:

function assoc_array_shuffle($array)
{
    $orig = array_flip($array);
    shuffle($array);
    foreach($array AS $key=>$n)
    {
        $data[$n] = $orig[$n];
    }
    return array_flip($data);
}

答案 4 :(得分:2)

尝试使用here中的fisher-yates算法:

function shuffle_me($shuffle_me) { 
   $randomized_keys = array_rand($shuffle_me, count($shuffle_me)); 
   foreach($randomized_keys as $current_key) { 
       $shuffled_me[$current_key] = $shuffle_me[$current_key]; 
   } 
   return $shuffled_me; 
} 

我必须为我的本科高级论文实现类似的东西,而且效果很好。

答案 5 :(得分:2)

function shuffle_assoc($array)
{
    $keys = array_keys($array);
    shuffle($keys);
    return array_merge(array_flip($keys), $array);
}

答案 6 :(得分:2)

Charles Iliya Krempeaux在此问题上有一个nice writeup,这个功能对我来说非常有用:

function shuffle_assoc($array)
{
    // Initialize
        $shuffled_array = array();


    // Get array's keys and shuffle them.
        $shuffled_keys = array_keys($array);
        shuffle($shuffled_keys);


    // Create same array, but in shuffled order.
        foreach ( $shuffled_keys AS $shuffled_key ) {

            $shuffled_array[  $shuffled_key  ] = $array[  $shuffled_key  ];

        } // foreach


    // Return
        return $shuffled_array;
}

答案 7 :(得分:0)

我试过最多的投票解决方案没有流行的洗牌清单。这是我为使其发挥作用而做出的改变。 我希望我的数组键从1开始。

 $list = array_combine(range(1,10),range(100,110));
 $shuffle_list =  shuffle_assoc($list);


function shuffle_assoc($list)
{
    if (!is_array($list)) return $list;

    $keys = array_keys($list);
    shuffle($list);
    $random = array();
    foreach ($keys as $k => $key) {
        $random[$key] = $list[$k];
    }
    return $random;
}

答案 8 :(得分:0)

使用随机播放的答案总是返回相同的顺序。这是使用random_int()的一种,每次使用的顺序都不同:

function shuffle_assoc($array)
{
    while (count($array)) {
        $keys = array_keys($array);
        $index = $keys[random_int(0, count($keys)-1)];
        $array_rand[$index] = $array[$index];
        unset($array[$index]);
    }

    return $array_rand;
}

答案 9 :(得分:0)

  $testArray = array('a' => 'apple', 'b' => 'ball', 'c' => 'cat', 'd' => 'dog');
  $keys = array_keys($testArray); //Get the Keys of the array -> a, b, c, d
  shuffle($keys); //Shuffle The keys array -> d, a, c, b
  $shuffledArray = array();
  foreach($keys as $key) {
    $shuffledArray[$key] = $testArray[$key]; //Get the original array using keys from shuffled array
  }
  print_r($shuffledArray);
  /*
  Array
  (
      [d] => dog
      [a] => apple
      [c] => cat
      [b] => ball
  )
  */