以同样的方式打乱 2 个 php 数组

发布于 2024-10-16 05:35:00 字数 673 浏览 4 评论 0原文

例如,我有这个:

$array['one'][0] = 0;
$array['one'][1] = 1;
$array['one'][2] = 2;
$array['one'][3] = 3;

$array['two'][0] = 00;
$array['two'][1] = 11;
$array['two'][2] = 22;
$array['two'][3] = 33;

我怎样才能将它们洗牌以获得类似的结果:

$array['one'][0] = 2;
$array['one'][1] = 1;
$array['one'][2] = 3;
$array['one'][3] = 0;

$array['two'][0] = 22;
$array['two'][1] = 11;
$array['two'][2] = 33;
$array['two'][3] = 00;

或任何其他随机顺序,但两者具有相同的“随机因子”?

例如,我希望将 $array['one'][0]$array['two'][0] 进行混洗以获得 $array ['one'][x]$array['two'][x]x 是随机键,但两个数组上的相同)。

I have this for example:

$array['one'][0] = 0;
$array['one'][1] = 1;
$array['one'][2] = 2;
$array['one'][3] = 3;

$array['two'][0] = 00;
$array['two'][1] = 11;
$array['two'][2] = 22;
$array['two'][3] = 33;

How can I shuffle them both to get something like:

$array['one'][0] = 2;
$array['one'][1] = 1;
$array['one'][2] = 3;
$array['one'][3] = 0;

$array['two'][0] = 22;
$array['two'][1] = 11;
$array['two'][2] = 33;
$array['two'][3] = 00;

Or any other random order, but having the same "random factor" in both?

For example, I want that $array['one'][0] and $array['two'][0] get shuffled to get $array['one'][x] and $array['two'][x] (x being a random key, but the SAME on both arrays).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岁月无声 2024-10-23 05:35:00
$count = count($array['one']);
$order = range(1, $count);

shuffle($order);
array_multisort($order, $array['one'], $array['two']);
  • 适用于具有任何类型元素的数组(也可以是对象和数组)。
  • 这种方式可以用于任意数量的数组(不仅仅是两个)。
  • 适用于重复值。
  • 干净的代码。
$count = count($array['one']);
$order = range(1, $count);

shuffle($order);
array_multisort($order, $array['one'], $array['two']);
  • Works with arrays with elements of any type (objects and arrays too).
  • This way may by used with any number of arrays (not only two).
  • Works with duplicated values.
  • Clean code.
百善笑为先 2024-10-23 05:35:00

像这样的东西可以工作。它与 Mimikry 的答案类似,只是即使数组一中碰巧有重复的值,这个答案也会起作用(这个答案不使用数组一的值作为临时数组的键)。

假设两个数组的大小相同。

$c = count($array['one']);
$tmp = array();
for ($i=0; $i<$c; $i++) {
  $tmp[$i] = array($array['one'][$i], $array['two'][$i]);
}
shuffle($tmp);
for ($i=0; $i<$c; $i++) {
  $array['one'][$i] = $tmp[$i][0];
  $array['two'][$i] = $tmp[$i][1];
}

Something like this could work. It's similar to Mimikry's answer except this one will work even if you happen to have duplicate values in array one (this one doesn't use values of array one as keys of the temporary array).

Assuming both arrays are of the same size.

$c = count($array['one']);
$tmp = array();
for ($i=0; $i<$c; $i++) {
  $tmp[$i] = array($array['one'][$i], $array['two'][$i]);
}
shuffle($tmp);
for ($i=0; $i<$c; $i++) {
  $array['one'][$i] = $tmp[$i][0];
  $array['two'][$i] = $tmp[$i][1];
}
呆萌少年 2024-10-23 05:35:00

希望我的理解是正确的。

对于上面的示例,此代码应该可以工作。但这非常hacky...

$array['one'][0] = 'A';
$array['one'][1] = 'B';
$array['one'][2] = 'C';
$array['one'][3] = 'D';

$array['two'][0] = 'AA';
$array['two'][1] = 'BB';
$array['two'][2] = 'CC';
$array['two'][3] = 'DD';

// save the dependencies in tmp array
foreach ($array['two'] as $key => $val) {
    $tmp[$array['one'][$key]] = $val;
}

shuffle($array['one']);

// restore dependencies in tmp2 array
foreach ($array['one'] as $key => $val) {
    $tmp2[$key] = $tmp[$val];
}

// overwrite with restore array
$array['two'] = $tmp2;

var_dump($array):

array(2) {
  ["one"]=>
  array(4) {
    [0]=>
    string(1) "B"
    [1]=>
    string(1) "A"
    [2]=>
    string(1) "C"
    [3]=>
    string(1) "D"
  }
  ["two"]=>
  array(4) {
    [0]=>
    string(2) "BB"
    [1]=>
    string(2) "AA"
    [2]=>
    string(2) "CC"
    [3]=>
    string(2) "DD"
  }
}

hopefully i get you right.

For your example above, this code should work. But it's pretty hacky...

$array['one'][0] = 'A';
$array['one'][1] = 'B';
$array['one'][2] = 'C';
$array['one'][3] = 'D';

$array['two'][0] = 'AA';
$array['two'][1] = 'BB';
$array['two'][2] = 'CC';
$array['two'][3] = 'DD';

// save the dependencies in tmp array
foreach ($array['two'] as $key => $val) {
    $tmp[$array['one'][$key]] = $val;
}

shuffle($array['one']);

// restore dependencies in tmp2 array
foreach ($array['one'] as $key => $val) {
    $tmp2[$key] = $tmp[$val];
}

// overwrite with restore array
$array['two'] = $tmp2;

var_dump($array):

array(2) {
  ["one"]=>
  array(4) {
    [0]=>
    string(1) "B"
    [1]=>
    string(1) "A"
    [2]=>
    string(1) "C"
    [3]=>
    string(1) "D"
  }
  ["two"]=>
  array(4) {
    [0]=>
    string(2) "BB"
    [1]=>
    string(2) "AA"
    [2]=>
    string(2) "CC"
    [3]=>
    string(2) "DD"
  }
}
谁人与我共长歌 2024-10-23 05:35:00

恕我直言,最佳实践如下:

$tmp = array_combine($array['one'],$array['two']);
shuffle($tmp);
$array['one'] = array_keys($tmp);
$array['two'] = array_values($tmp);

这是清晰的代码,并且应该很快。不需要像其他答案一样重新发明轮子。

The best practice, imho, is as follows:

$tmp = array_combine($array['one'],$array['two']);
shuffle($tmp);
$array['one'] = array_keys($tmp);
$array['two'] = array_values($tmp);

This is clear code and should be fast. No need to reinvent the wheel like in some other answers.

萌能量女王 2024-10-23 05:35:00

这不是多维数组的示例,但它非常适合以相同的方式和随机播放对多个普通数组进行排序。也许它也可以适用于多维数组。它确实假设所有数组的长度相同。

$array_count = count($array_1);

for($i=0;$i<=($array_count-1);$i++){
    $temp_array[$i] = $i;
}

shuffle($temp_array);

for($i=0;$i<=($array_count-1);$i++){
    $o = $temp_array[$i];
    $array_1_sorted[$i]=$array_1[$o];
    $array_2_sorted[$i]=$array_2[$o];
    $array_3_sorted[$i]=$array_3[$o];
}

这将为您提供新的数组,这些数组都以相同的随机方式排序。然后,您可以将旧数组设置为与新数组相同或保持它们不变。

This isn't an example with multidimensional arrays but it works great for sorting multiple normal arrays the same way and with shuffle. Maybe it could be adapted for multidimensional arrays too. It does assume all arrays are the same length.

$array_count = count($array_1);

for($i=0;$i<=($array_count-1);$i++){
    $temp_array[$i] = $i;
}

shuffle($temp_array);

for($i=0;$i<=($array_count-1);$i++){
    $o = $temp_array[$i];
    $array_1_sorted[$i]=$array_1[$o];
    $array_2_sorted[$i]=$array_2[$o];
    $array_3_sorted[$i]=$array_3[$o];
}

This will give you new arrays which are all sorted the same random way. You could then set the old arrays equal to the new ones or keep them in-tact.

多孤肩上扛 2024-10-23 05:35:00

不确定您到底想做什么,但最好的选择可能是将数组 1 和数组 2 放入多维数组中,然后随机主数组。这将为您提供随机排列的值,但每个键中将包含每个数组的值。如果您能提供有关您正在做的事情的更多详细信息,我们可以为您提供更具体的答案。

Not sure exactly what you are trying to do, but your best bet is probably to put array 1 and array 2 into a multi-dimensional array and then random the primary array. That will give you a random arrangement of values, but within each key will be the values of each array. We could give you more specific answers if you can provide a bit more detail of exactly what you are doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文