0

我有一个像这样的多维数组:

Array
(
    [site1] => Array
        (
            [0] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data
                )

            [1] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data

                )

            [2] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data

                )

    [site2] => Array
        (
            [0] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data
                )

            [1] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data
                )

            [2] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data
                )
         )
)

我正在尝试随机化每个站点([site1]、[site2])的数据,但不要在站点之间混合数据。所以这就像第二次轮胎随机化。

例如,在随机化之后,[site1] 的位置 [0] 将具有不同的数据,可能是来自较早位置 [3] 的数据。

任何想法如何做到这一点?

4

3 回答 3

3

您将函数映射到数组:shuffle

$shuffle = function($array) {
    $r = shuffle($array);
    return $array;
};

$sites = array_map($shuffle, $sites);

或者:

foreach ($sites as &$site) shuffle($site);
unset($site);
于 2012-09-23T21:38:18.333 回答
1
foreach($sites as $i => $site) {
   shuffle($sites[$i]);
}
于 2012-09-23T21:37:40.327 回答
0

另一种(不如随机播放)方式;-)

foreach ($site as $key => $data) {
  $values = array();
  $rand   = array_rand($data, count($data));
  for ($i = 0; $i < count($rand); $i++)) {
    $values[] = $site[$key][$rand[$i]];  
  }
  $site[$key] = $values;
}
于 2012-09-23T21:50:41.373 回答